Jump to content


Photo

Broken script that works beatuifully. But HOW DOES IT WORK?


  • Please log in to reply
1 reply to this topic

#1 doramia

doramia
  • Members
  • 1 posts

Posted 04 August 2012 - 02:34 PM

I wanted a simple scrip that wrote the numlock status to the screen, and sent a right arrow if numlock was off.
This works fine, but I was hoping to optimise it. It was a mistype that ended up working, but I'm afraid I dont know how it actually works.

~*NumLock::
if GetKeyState("NumLock", "T")
Progress, 0 B1 W200 H28 ZH0 FS11 WS900 Y700 CTFF0000, NUM LOCK ON
Loop
{
Send {Right}
if GetKeyState("NumLock", "T")
break
Progress, 0 B1 W200 H28 ZH0 FS11 WS900 Y700 CT0000FF, NUM LOCK OFF
SetTimer, OSD_OFF, -2000
}
Return

OSD_OFF:
Progress, off
Return

#2 G. Sperotto

G. Sperotto
  • Members
  • 467 posts

Posted 04 August 2012 - 04:00 PM

Hi Doramia.

You are looking for some explanations on how the code works?

Comments in green:

~*NumLock::
[color=#008000]; ~ is hotkey modifier. When the hotkey triggger (numlock) is pressed, the signal will not only be used by AHK but by any other application that catches it (without this modifier, AHK blocks the signal so that other applications don't see it). * is also a modifier. If you are holding control while you press numlock, the hotkey will still be triggered (without this modifier, pressing a combination of keys that contains the trigger might not activate the trigger).[/color]
if GetKeyState("NumLock", "T") [color=#008000]; If the current state of numlock is toggled (active), the code bellow will execute. Otherwise, it won't. Note that due to the modifiers above, this check probably comes AFTER the system has changed numlock due to the very keypress of the hotkey.[/color]
Progress, 0 B1 W200 H28 ZH0 FS11 WS900 Y700 CTFF0000, NUM LOCK ON [color=#008000]; Displays a progress bar with the text "Numlock On".[/color]
Loop [color=#008000]; repeats the code inside the braces bellow untill "break" is executed.[/color]
{
Send {Right} [color=#008000]; Send a right arrow keystroke.[/color]
if GetKeyState("NumLock", "T") [color=#008000]; If the current state of numlock is toggled (active), the code bellow will execute. [/color]
break [color=#008000]; Terminates the loop. This code executes only if the condition above is true (numlock is active).[/color]
Progress, 0 B1 W200 H28 ZH0 FS11 WS900 Y700 CT0000FF, NUM LOCK OFF [color=#008000]; Displays a progress bar with the text "NumLock Off". Due to the conditional above, this code will only execute if the state of numlock is off (inactive) because if it is active, the loop will have reached break before this line can get to be executed.[/color]
SetTimer, OSD_OFF, -2000 [color=#008000]; The label OSD_OFF is set to be executed after 2 seconds (2000 microseconds). It should only execute once, but the loop keps turning the timer on.[/color]
}
Return [color=#008000]; Ends the auto-execution section (code bellow will only be executed if somehow called in the code above - which is done by the settimer command).[/color]

OSD_OFF:
Progress, off [color=#008000]; Destroys the progress bar.[/color]
Return [color=#008000]; Ends the routine.[/color]
In other words:

When you press "NumLock", the signal goes to the system (becuase of modifier ~) and the system turns numlock on/off. After that (emphasis on AFTER THAT), the code checks to see if the numlock key is toggled (something that if so, should be due to the numlock keypress itself), and if it is found to be now active, displays a progress bar named "NUM LOCK ON". After that, it sends a rightarrow keystroke (inside the loop) and checks the state of numlock again. If it is found to be active yet again, the loop is broken and all the routines end untill numlock is called again. If it is not found to be active in this second check, the code repeats indefinitely a routine of checking numlock, displaying a progress named "NUM LOCK OFF" and destroying that progress right after.

A much more clean code would be:

~*NumLock::
if GetKeyState("NumLock", "T")
{
Send {Right}
Progress, 0 B1 W200 H28 ZH0 FS11 WS900 Y700 CTFF0000, NUM LOCK ON
}
Else
Progress, 0 B1 W200 H28 ZH0 FS11 WS900 Y700 CT0000FF, NUM LOCK OFF

Note that your code would continuously send multiple RightArrow keystrokes if the numlock was turned off, and the code above will only send it once and only if numlock was turned on (as per your description).

Best wishes :wink: