I want to write a small app with AHK, but I'm facing difficulties in understanding the very basics of the flow control and the syntax. So basically the question can be understood as - how do I write a simple program in AHK?
Here is a very simple program which must toggle the flag "pan_on" with the right mouse button. Then do something depending on this value.
pan_on := false
Esc:: ExitApp
Rbutton::
tooltip, "button pressed"
pan_on := true
return
Rbutton up::
tooltip, "released"
pan_on := false
return
if (pan_on = true)
{
tooltip, "XXXXXXXXEngage"
}
The blocks which start with the "Rbutton " line are working correctly. The problem however is that the last block with the conditional statement is NEVER executed. Adding endless loop around last block or all the script doeas not help either.
I am mostly familiar with such programming style (here using Python syntax):
pan_on = False
while True:
pan_on = False
if key_down[Esc]:
break
if key_down[Rbutton]:
pan_on = True
if key_up[Rbutton]:
pan_on = False
...
if pan_on:
draw_something ()
So I can't think of anything much defferent from that to write a program, but seems that autohotkey is something "special" in this sense and one must use other approach? In the first AHK example it seems that "Rbutton::" is always in ready state and I think of it as a conditional "if key_down" inside a endless loop, but I must be wrond about it?
So how do I make this simple app, possibly without changing the code style? Where is the main loop in AHK? How does the control flow work, how to manage entry points for parts of code which is run? If I just write everything after "Rbutton::" it works but it is not how one writes programs, and will be very difficult to develop the logic further.
As far as control flow, your if
statement is never reached. As far as I understand, the interpreter first reads in an AutoExec section at the top of the script which handles settings, then acts upon Hotkeys and Directives, then executes line for line code, until it hits a Return. You would also need to loop your code to continually check for the Value was true or it would simply check once and move on.
pan_on := false
loop {
if (pan_on = true)
tooltip, "XXXXXXXXEngage"
}
Esc:: ExitApp
Rbutton::
tooltip, "button pressed"
pan_on := true
return
Rbutton up::
tooltip, "released"
pan_on := false
return