How can I dynamically create hotkeys with HotKey that calls a function with additional paramaters? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
IMTheNachoMan
Posts: 59
Joined: 01 Mar 2022, 17:07
Contact:

How can I dynamically create hotkeys with HotKey that calls a function with additional paramaters?

23 Sep 2023, 22:58

Below is a basic example of what I am trying to do.

I commented out the 3 lines because none of them work.

I am dynamically creating hotkeys from config data using HotKey. The hotkey will call a function. When the hotkey is triggered, I want additional data to be sent to the function.

Code: Select all

#Requires AutoHotkey v2.0
#Warn Unreachable, off
#SingleInstance Force

data := ["^!+a", "^!+b", "^!+c"]

for i, v in data
{
    ; HotKey v, (ThisHotKey) => dingo(i, v)
    ; HotKey v, (ThisHotKey) => dingo.bind(i, v)
    ; HotKey v, dingo.bind(i, v)
}

dingo(index, data)
{
    msgbox index " " data
    return
}
User avatar
boiler
Posts: 17194
Joined: 21 Dec 2014, 02:44

Re: How can I dynamically create hotkeys with HotKey that calls a function with additional paramaters?

23 Sep 2023, 23:09

Hotkey documentation wrote:If Action is a function, it is called with one parameter, the name of the hotkey.
User avatar
boiler
Posts: 17194
Joined: 21 Dec 2014, 02:44

Re: How can I dynamically create hotkeys with HotKey that calls a function with additional paramaters?

23 Sep 2023, 23:15

This achieves the same result:

Code: Select all

#Requires AutoHotkey v2.0
#Warn Unreachable, off
#SingleInstance Force

data := ["^!+a", "^!+b", "^!+c"]

for i, v in data
    HotKey v, dingo

dingo(thisHotkey) {
	for i, v in data
		if (v = thisHotkey)
			index := i
    MsgBox index " " thisHotkey
}
IMTheNachoMan
Posts: 59
Joined: 01 Mar 2022, 17:07
Contact:

Re: How can I dynamically create hotkeys with HotKey that calls a function with additional paramaters?

23 Sep 2023, 23:23

boiler wrote:
23 Sep 2023, 23:09
Hotkey documentation wrote:If Action is a function, it is called with one parameter, the name of the hotkey.
Yes, I saw that. So I'm trying to figure out how to call the function with additional data.
IMTheNachoMan
Posts: 59
Joined: 01 Mar 2022, 17:07
Contact:

Re: How can I dynamically create hotkeys with HotKey that calls a function with additional paramaters?

23 Sep 2023, 23:25

boiler wrote:
23 Sep 2023, 23:15
This achieves the same result:

Code: Select all

#Requires AutoHotkey v2.0
#Warn Unreachable, off
#SingleInstance Force

data := ["^!+a", "^!+b", "^!+c"]

for i, v in data
    HotKey v, dingo

dingo(thisHotkey) {
	for i, v in data
		if (v = thisHotkey)
			index := i
    MsgBox index " " thisHotkey
}
So I thought that too. But there must be some more efficient way? It would be very slow to have to loop through the data every time a hotkey is pressed.

There must be some mechanism to pass data to a hotkey function?
User avatar
boiler
Posts: 17194
Joined: 21 Dec 2014, 02:44

Re: How can I dynamically create hotkeys with HotKey that calls a function with additional paramaters?

23 Sep 2023, 23:29

IMTheNachoMan wrote: But there must be some more efficient way? It would be very slow to have to loop through the data every time a hotkey is pressed.

Code: Select all

#Requires AutoHotkey v2.0
#Warn Unreachable, off
#SingleInstance Force

data := Map("^!+a", 1, "^!+b", 2, "^!+c", 3)

for i, v in data
    HotKey i, dingo

dingo(thisHotkey) {
    MsgBox data[thisHotkey] " " thisHotkey
}
IMTheNachoMan
Posts: 59
Joined: 01 Mar 2022, 17:07
Contact:

Re: How can I dynamically create hotkeys with HotKey that calls a function with additional paramaters?

23 Sep 2023, 23:33

@boiler

Hurmph. That is what I am currently doing. I guess it works. I just thought maybe there would be a better way...
ntepa
Posts: 434
Joined: 19 Oct 2022, 20:52

Re: How can I dynamically create hotkeys with HotKey that calls a function with additional paramaters?  Topic is solved

24 Sep 2023, 00:13

Code: Select all

#Requires AutoHotkey v2.0
#Warn Unreachable, off
#SingleInstance Force

data := ["^!+a", "^!+b", "^!+c"]

for i, v in data
{
	Hotkey v, dingo.Bind(i, v)
}

dingo(index, data, *) ; * allows extra parameters to be ignored
{
    msgbox index " " data
    return
}
IMTheNachoMan
Posts: 59
Joined: 01 Mar 2022, 17:07
Contact:

Re: How can I dynamically create hotkeys with HotKey that calls a function with additional paramaters?

24 Sep 2023, 10:30

@ntepa

Ah. So the , * is needed in the bind. I didn't know that.

Do you mind explaining why? I get that bind returns a BoundFunc with "pre defined parameters". So why is the , * needed?
User avatar
boiler
Posts: 17194
Joined: 21 Dec 2014, 02:44

Re: How can I dynamically create hotkeys with HotKey that calls a function with additional paramaters?

24 Sep 2023, 10:35

It's because he bound two additional parameters in addition to the default parameter that I posted about. As his comment in his script points out, the * tells it to ignore any additional parameters sent. Notice that you can actually have it accept that parameter instead of ignoring it:

Code: Select all

#Requires AutoHotkey v2.0
#Warn Unreachable, off
#SingleInstance Force

data := ["^!+a", "^!+b", "^!+c"]

for i, v in data
{
	Hotkey v, dingo.Bind(i, v)
}

dingo(index, data, thisHotkey)
{
    msgbox index " " data " " thisHotkey
    return
}
IMTheNachoMan
Posts: 59
Joined: 01 Mar 2022, 17:07
Contact:

Re: How can I dynamically create hotkeys with HotKey that calls a function with additional paramaters?

24 Sep 2023, 10:42

@boiler

It appears that when you use bind with hotkey, the called function has additional parameters then what you bound. I didn't know/realize that. I now see it sends an additional parameter of the hotkey.
User avatar
boiler
Posts: 17194
Joined: 21 Dec 2014, 02:44

Re: How can I dynamically create hotkeys with HotKey that calls a function with additional paramaters?

24 Sep 2023, 11:33

It sends the default one in addition to the newly bound ones. Binding new parameters doesn't remove the parameter that is already associated with a hotkey function.
IMTheNachoMan
Posts: 59
Joined: 01 Mar 2022, 17:07
Contact:

Re: How can I dynamically create hotkeys with HotKey that calls a function with additional paramaters?

24 Sep 2023, 13:08

@boiler Yeah, I see that now. I missed this part of the documentation from https://www.autohotkey.com/docs/v1/misc/Functor.htm#BoundFunc:
When the BoundFunc is called, it calls the function or method to which it is bound, passing any bound parameters followed by any which were passed by the caller.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: mikeyww, smogmanus, Xeilous and 23 guests