Creates context-sensitive hotkeys and hotstrings. Such hotkeys perform a different action (or none at all) depending on the result of an expression.
#If Expression
Any valid expression.
Any valid expression may be used to define the context in which a hotkey should be active. For example:
#If WinActive("ahk_class Notepad") or WinActive(MyWindowTitle) #Space::MsgBox "You pressed Win+Spacebar in Notepad or " MyWindowTitle
Like the #IfWin directives, #If is positional: it affects all hotkeys and hotstrings physically beneath it in the script. #If and #IfWin are also mutually exclusive; that is, only the most recent #If or #IfWin will be in effect.
To turn off context sensitivity, specify #If or any #IfWin directive but omit all the parameters. For example:
#If
When the key combination which forms a hotkey is pressed, the #If expression is evaluated to determine if the hotkey should activate. The system may not respond to keyboard or mouse input until expression evaluation completes or times out. Sending keystrokes or mouse clicks while the expression is being evaluated (such as from a function which it calls) may cause complications and should be avoided.
The expression may also be evaluated whenever the program needs to know whether the hotkey is active. For example, the #If expression for a custom combination like a & b::
might be evaluated when the prefix key (a
in this example) is pressed, to determine whether it should act as a custom modifier key.
For the reasons described above, the expression should be written to complete quickly and without side-effects.
A_ThisHotkey and A_TimeSinceThisHotkey are set based on the hotkey for which the current #If expression is being evaluated.
A_PriorHotkey and A_TimeSincePriorHotkey temporarily contain the previous values of the corresponding "This" variables.
Most behavioural properties of the #IfWin directives also apply to #If.
#IfTimeout may be used to override the default timeout value.
; Example 1: Adjust volume by scrolling the mouse wheel over the taskbar. #If MouseIsOver("ahk_class Shell_TrayWnd") WheelUp::Send "{Volume_Up}" WheelDown::Send "{Volume_Down}" MouseIsOver(WinTitle) { MouseGetPos ,, Win return WinExist(WinTitle " ahk_id " Win) } ; Example 2: Simple word-delete shortcuts for all Edit controls. #If ActiveControlIsOfClass("Edit") ^BS::Send "^+{Left}{Del}" ^Del::Send "^+{Right}{Del}" ActiveControlIsOfClass(Class) { FocusedControl := ControlGetFocus("A") FocusedControlHwnd := ControlGetHwnd(FocusedControl, "A") FocusedControlClass := WinGetClass("ahk_id " FocusedControlHwnd) return (FocusedControlClass=Class) } ; Example 3: Context-insensitive hotkey. #If Esc::ExitApp ; Example 4: Dynamic hotkeys. Requires Example 1. NumpadAdd:: Hotkey "If", 'MouseIsOver("ahk_class Shell_TrayWnd")' if (doubleup := !doubleup) Hotkey "WheelUp", "DoubleUp" else Hotkey "WheelUp", "WheelUp" return DoubleUp: Send "{Volume_Up 2}" return