bind function object to pass parameters with hotkey command

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
LarryS
Posts: 3
Joined: 12 Jun 2022, 10:27
Contact:

bind function object to pass parameters with hotkey command

Post by LarryS » 12 Jun 2022, 11:08

Autohotkey v2.0-beta.3
Hello I want to call a function that receives parameters when a hotkey is pressed. Here is a simplified version. When key is pressed, gives "Too many parameters" execution error. Please explain why the error and show the code that will work.

Code: Select all

#singleInstance force
#requires AutoHotkey v2.0-b 

; tests function object binding with hotkeys.

; doObject := do.bind("z")
		doObject := func("do").bind("z")
hotkey "^+a", doObject
return

do(a) {
msgBox a
return
}
PS. Very much enjoy and prefer v2. Thank you!

Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: bind function object to pass parameters with hotkey command

Post by Helgef » 12 Jun 2022, 11:42

Hotkey - Action wrote: If Action is a function, it is called with one parameter, the name of the hotkey.
src
Also see :arrow: BoundFunc.
Example,

Code: Select all

#singleInstance force
#requires AutoHotkey v2.0-b 

; tests function object binding with hotkeys.


hotkey "^+a", do.bind( ,'z')
return

do(this_hotkey, a) {
	msgBox this_hotkey . ' ' . a
return
}
Cheers.

LarryS
Posts: 3
Joined: 12 Jun 2022, 10:27
Contact:

Re: bind function object to pass parameters with hotkey command

Post by LarryS » 12 Jun 2022, 14:46

Perfect! Elegant. Never would have caught that subtlety. Thank you so much for your instant solution.

Post Reply

Return to “Ask for Help (v2)”