Hotkey If <condition> breaking Send a hotkey

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Hotkey If <condition> breaking Send a hotkey

Post by JnLlnd » 09 Dec 2022, 11:04

I have a situation where a hotkey command calls another hotkey command using the Send command (in real life, this is used in Quick Access Popup "Snippets" that, in macro mode, can include hotkeys). Normally, a hotkey can call a second hotkey as shown in this piece of code. Pressing Alt+F1 will show the message box for Alt+F1 then show it a second time for Alt+F2.

Code: Select all

#requires AutoHotkey v1.1
#SingleInstance,Force

Hotkey, !F1, DoIt
Hotkey, !F2, DoIt
return

DoIt:
MsgBox, %A_ThisLabel% with: %A_ThisHotkey%
if (A_ThisHotkey = "!F1")
	Send, !{F2}
return
But when these hotkeys are monitored by a Hotkey, If <condition>, the second hotkey is not executed, even if the condition is true. Would it be a bug?

Code: Select all

#requires AutoHotkey v1.1
#SingleInstance,Force

; Enable hotkeys only if CanDoIt() returns true
Hotkey, If, CanDoIt()
	Hotkey, !F1, DoIt
	Hotkey, !F2, DoIt
Hotkey, If

; Handle for the "Hotkey, If" condition
#If, CanDoIt()
#If
return

DoIt:
MsgBox, %A_ThisLabel% with: %A_ThisHotkey%
SendMode Event ; Input InputThenPlay  Play
if (A_ThisHotkey = "!F1")
	Send, !{F2}
return

CanDoIt() ; name of the function must match the #If handle above
{
	return true
	; return false
}
I tried with other Send modes (Input, InputThenPlay, Play) with the same result. I also tried with the IfWinActive command (like Hotkey, IfWinActive, ahk_group TestGroup) with the same result.
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Hotkey If <condition> breaking Send a hotkey

Post by swagfag » 09 Dec 2022, 15:08

context-sensitive hotkey are keyboard-hook hotkeys. artificial Sends dont trigger hook hotkeys, unless their SendLevel exceeds the hotkeys' #InputLevel.
use SendLevel 1(or higher if needed) and not any of the Play variants. not a bug

User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Re: Hotkey If <condition> breaking Send a hotkey

Post by JnLlnd » 09 Dec 2022, 15:28

Yes! SendLevel 1 made it work. Thanks swagfag!

In my test, I found that the command needs to be placed just before the Send command. It did not work when placed in the auto-execute section.
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Hotkey If <condition> breaking Send a hotkey

Post by swagfag » 09 Dec 2022, 15:52

for hotkeys/-strings exclusively, u cant. its designed that way
Every newly launched hotkey or hotstring thread starts off with a send level equal to the input level of the hotkey or hotstring.

User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Re: Hotkey If <condition> breaking Send a hotkey

Post by JnLlnd » 09 Dec 2022, 15:54

I've read that but did not get it... Got it now. Thanks :-)
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey

Post Reply

Return to “Ask for Help (v1)”