New test build - Hotkey, If, % FunctionObject

Community news and information about new or upcoming versions of AutoHotkey
lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

New test build - Hotkey, If, % FunctionObject

17 Dec 2016, 22:26

v1.1.24.04-3+g4a9ad3f

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
Other changes: Hotkey, If, x now throws an exception instead of setting ErrorLevel if x is invalid. Note that it already raised an error at load-time if x was invalid and did not contain a variable or expression. Usually a non-zero ErrorLevel would go unnoticed.

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
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: New test build - Hotkey, If, % FunctionObject

18 Dec 2016, 03:20

Excellent addition.
There is no matching #if, how come that isn't needed? (Not implying that I understand enough about the implemention that I understand why it's needed in other cases.)

Cheers!
Getfree
Posts: 231
Joined: 12 Oct 2014, 18:00

Re: New test build - Hotkey, If, % FunctionObject

18 Dec 2016, 20:09

Awesome. I'll try this ASAP.
Will it be added to AHK v2 ?
Helgef wrote:There is no matching #if, how come that isn't needed? (Not implying that I understand enough about the implemention that I understand why it's needed in other cases.)
When passing a string as the #IF expression, that string must match a hard-coded one because AHK can't parse/eval new expressions at runtime.
Whereas when using a functor there's no expression that needs to be evaluated, it's just an object reference.
lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: New test build - Hotkey, If, % FunctionObject

19 Dec 2016, 03:33

I periodically merge the main v1 branch into the v2 branch, so yes.

However, these changes aren't in the main v1 branch yet.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: New test build - Hotkey, If, % FunctionObject

19 Dec 2016, 13:34

Getfree wrote:When passing a string as the #IF expression, that string must match a hard-coded one because AHK can't parse/eval new expressions at runtime.
Whereas when using a functor there's no expression that needs to be evaluated, it's just an object reference.
Thank you.

Return to “Announcements”

Who is online

Users browsing this forum: No registered users and 22 guests