If key is pressed

Syntax

ifk[-] keycode [toggled]
(tab)	statement
(tab)	...
[else
(tab)	statement
(tab)	... ]
...

 

Can be single line:

ifk[-](keycode [toggled]) statement ...
[else statement ...]

 

Parts

keycode - QM key code. Also can be virtual-key code enclosed in parentheses. Also can test mouse buttons. Virtual-key codes for mouse buttons: 1 left, 2 right, 4 middle, 5 X1, 6 X2.

toggled - if nonzero, checks whether the key is toggled.

statements - any statements.

Options: - not.

 

Remarks

Similar to if. If the specified key is pressed, executes statements after ifk and skips statements after else (if any). Otherwise, skips statements after ifk and executes statements after else.

 

Not all keys and key combinations give correct result. For example, on some operating systems you cannot test the Pause key.

 

On Vista, if the active window has higher integrity level (eg QM - standard user, window - administrator), ifk works only with some keys: modifier keys (Ctrl, Shift, Alt, Win), lock keys (CapsLock, NumLock, ScrollLock), Back, Tab, Enter, Esc, and mouse buttons.

 

On Vista, ifk can reliably check toggled state only for lock keys. The toggled state for other keys is process-specific or thread-specific.

 

Internally ifk uses QM function RealGetKeyState. You can use it in macros instead of ifk.

 

See also: GetMod

 

Examples

ifk(F2) bee ;;if key F2 pressed, beep
ifk(K 1) key K ;;if key CapsLock toggled, press CapsLock
ifk((1)) bee ;;if left mouse button pressed, beep

 Repeatedly execute some code; stop when key F12 is pressed:
rep
	...
	ifk(F12) break