Hotkey command on function object passed as parameter

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
bschamel
Posts: 15
Joined: 08 Aug 2016, 11:18

Hotkey command on function object passed as parameter

22 Mar 2017, 15:29

I'm trying to run the Hotkey command where the target "label" is actually a function object that was passed in as a parameter to the current function:

Here's the whole file, as it is right now:

Code: Select all

#SingleInstance Force
f82bound := Func("F82").Bind("Bound")
F8::
	DoubleKeyPressTo(f82bound, "F8")
	return
F82(arg)
{
	MsgBox %arg%
}
DoubleKeyPressTo(funcObj, key = "")
{
	if not key
		key := A_ThisHotkey
	Hotkey, %key%, %funcObj%
	Sleep 2000
	Hotkey, %key%, %funcObj%, Off
}
Here, the first time i hit F8, it runs as from the f8 label (as it should).

However, if I press the F8 key again during the 2 second window there, the script doesn't do anything (when, as I understand it, it should launch the F82bound function object)

I also tried replacing the function call with just the command itself - for example:

Code: Select all

F8::
	;~ DoubleKeyPressTo(f82bound, "F8")
	Hotkey, F8, %f82bound%
	return
It will behave as expected - Running the F82Bound function on the second press.

What am I doing wrong?
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Hotkey command on function object passed as parameter

22 Mar 2017, 16:15

Try inserting this as first or second line :

Code: Select all

#MaxThreadsPerHotkey, 2
I hope that helps.
bschamel
Posts: 15
Joined: 08 Aug 2016, 11:18

Re: Hotkey command on function object passed as parameter

23 Mar 2017, 13:21

That worked, but is there a way that I can modify just the function so that it'll work? As I understand it, the # directives are things that are taken into account when the script is compiled, and can't be changed.

It also worked if I moved the Hotkey command to outside the function, too, so I have to imagine it's possible to work in the function.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Hotkey command on function object passed as parameter

23 Mar 2017, 16:27

I have some code which I posted today, which could potentially be of use. Best of luck.

log hotkeys, get extended hotkey info, interact with the *right* window (Hotkey command with FunctionObject example) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=29560
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: Hotkey command on function object passed as parameter

23 Mar 2017, 22:58

It also worked if I moved the Hotkey command to outside the function, too
It has nothing to do with whether the command is inside or outside the function.

Do you understand why #MaxThreadsPerHotkey was required?

When you first press F8, it calls DoubleKeyPressTo. This function calls the Hotkey command, and then it sleeps for 2 seconds. While it is sleeping, the F8 hotkey already owns one thread, and cannot start another if the maximum is one thread per hotkey.

If you want to avoid the directive, there are two solutions:
  • Use the Tn option, as described in the documentation for the Hotkey command.
  • Use a timer instead of Sleep, so that the hotkey's thread exits immediately after changing the hotkey.
Note that there is another error in your script: you changed the F8 hotkey and then instead of changing it back, you turned it off completely.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Exies, JoeWinograd, scriptor2016 and 97 guests