Added Hotkey, If, % FunctionObject.
Subsequently created hotkeys will only execute if calling the given function object yields a non-zero number.
This works much the same as #If Expression, but using a function object instead of a hard-coded expression. Any number of function objects can be created at runtime.
FunctionObject must be a plain variable containing an object with a call method; typically a BoundFunc. The function or call method is called with one parameter: A_ThisHotkey.
Each object is considered a unique context for hotkeys. To modify a hotkey after having used Hotkey, If to change the context, you must change the context back by passing the original object to Hotkey, If, % FunctionObject (i.e. if it was a bound function, passing a new object bound to the same function would not allow you to modify existing hotkeys).
Once passed to the Hotkey command, the object will never be deleted. It is not possible to delete hotkeys, and the presence of a hotkey requires that a reference to the object be kept permanently. (Currently the object reference is kept even if there are no hotkeys, to simplify the implementation. When the process exits, all memory allocated by the process is reclaimed by the OS.)
For example, this GUI allows you to register primitive three-key combination hotkeys:
Code: Select all
Gui Add, Text, xm, Prefix key:
Gui Add, Edit, yp x100 w100 vPrefix, Space
Gui Add, Text, xm, Suffix hotkey:,
Gui Add, Edit, yp x100 w100 vSuffix, f & j
Gui Add, Button, Default, Register
Gui Show
return
ButtonRegister() {
global
Gui Submit, NoHide
local fn
fn := Func("HotkeyShouldFire").Bind(Prefix)
Hotkey If, % fn
Hotkey % Suffix, FireHotkey
}
HotkeyShouldFire(prefix, thisHotkey) {
return GetKeyState(prefix)
}
FireHotkey() {
MsgBox %A_ThisHotkey%
}
GuiClose:
GuiEscape:
ExitApp
Note that Hotkey, If, % fn does not set ErrorLevel = 1 when fn is an object on current versions, as it only checks the string value of the parameter, which is empty. Therefore the hotkey will register, but as a global hotkey, not context sensitive.
Download