I'm trying to code some interaction between a pair of hotkeys and an input hook that goes something like what follows:
I'll have a pair of down/up hotkeys defined for a certain key (both at Input Level 100). In their function bodies I'll make calls to SendEvent "{Blind}{Thiskey DownR/Up}" (both at Send Level 100).
At some point during execution, I'll have an input hook in progress with OnKeyDown/Up callbacks defined for that same key.
What I want to do is: I want to control whether the input hook will capture that key's down/up inputs by setting the input hook's MinSendLevel back and forth from 100 to 101 (that is, without having to turn off/on the input hook).
By reading the documentation for InputHook, it seemed like a simple enough task, so I set out to do it and the code structure looks somewhat like this:
Code: Select all
#Requires AutoHotkey v2.-
; These are here just to support debugging
ttx := A_ScreenWidth //3
tty := A_ScreenHeight//3
ih := InputHook("I101")
ih.KeyOpt("z", "+N")
ih.OnKeyDown := OnKeyDown
ih.OnKeyUp := OnKeyUp
ih.VisibleText := True
OnKeyDown(ih, vk, sc) {
; These are here just to support debugging
ToolTip , , , 2
ToolTip "ih Down `nA_SendLevel: " A_SendLevel, ttx, tty, 1
}
OnKeyUp(ih, vk, sc) {
; These are here just to support debugging
ToolTip "ih Up `nA_SendLevel: " A_SendLevel, ttx, tty+(18)*2, 2
SetTimer ()=>(ToolTip(,,,1),ToolTip(,,,2)), -4000
}
#InputLevel 100
z::
d(hk) {
; These are here just to support debugging
ToolTip , , , 4
ToolTip "hk Down `nA_SendLevel: " A_SendLevel, ttx-200, tty, 3
; At SendLevel 100
SendEvent "{Blind}{z DownR}"
}
z Up::
u(hk) {
; These are here just to support debugging
ToolTip "hk Up `nA_SendLevel: " A_SendLevel, ttx-200, tty+(18)*2, 4
SetTimer ()=>(ToolTip(,,,3),ToolTip(,,,4)), -4000
; At SendLevel 100
SendEvent "{Blind}{z Up}"
}
;
; At some point during execution:
ih.Start() ; It starts out with MinSendLevel 101, so it should neither capture z Down, nor z Up from my hotkeys
; At some later time during execution:
ih.MinSendLevel := 100 ; it should start capturing z Down/Up inputs from the hotkeys
; And then:
; ih.MinSendLevel := 101 ; it should stop capturing z Down/Up inputs without turning the input hook off
However, what happens is pretty strange: the input hook always captures Key Up events, even when MinSendLevel is set to 101.
What boggles my mind is that when it comes to Key Down events, the interaction hotkeys/input hook seems to work perfectly fine: the input hook will ignore or capture Key Down events according to how its MinSendLevel compares to the event's Send Level, exactly as you would expect; as for Key Up events, it will always capture them...
To add some more context, here's a picture of the crime scene at MinSendLevel 101:
And here's a pic of it at MinSendLevel 100:
At first glance, you'd think it's behaving normally at MinSendLevel 100. However, looking closely at the picture, you can see that it's still misbehaving and capturing the Key Up event before, as well as AFTER, SendEvent is called.
That's pretty strange to me and I can't put my finger on what is going on... Is this a bug? Am I missing something from the documentation? I've been scratching my head for so long I think I'm going bald...
Can somebody help me, please?
Thanks in advance, anyways!
Btw, I'm using version 2.0.2 and had no other scripts active during the tests.