v2 keyboard hook sometimes not capturing key actions

Report problems with documented functionality
synthmusic
Posts: 8
Joined: 13 Nov 2021, 01:54

v2 keyboard hook sometimes not capturing key actions

Post by synthmusic » 27 Jul 2022, 00:30

v2.0-beta.5

I've been working on a script that uses keyboard hooks so there can be reactions to the key down and key up, and I notice that any time the computer is under a little bit of load (like opening a busy website or working under battery saver because the battery is almost dead) some inputs are not captured at all. I can consistently reproduce it, though sometimes via artificial load on the computer.

Here's a script that maps 2 keys for down and up. If I hit & release those 2 keys at the same time a number of times in a row, (again with a somewhat busy computer) actions will not get captured on some attempts. I used Send functions with arbitrary numbers to make sure it wasn't a problem with sending the same key being pressed.

Code: Select all

#SingleInstance Force
KeyHistory(200)

hotkey "$*j", Send1, "On"
hotkey "$*j Up", Send2, "On"
hotkey "$*k", Send3, "On"
hotkey "$*k Up", Send4, "On"

Send1(key) {
    Send("1")
    ; Send("11111")
    ; Send(key)
}
Send2(key) {
    Send("2")
    ; Send("22222")
    ; Send(key)
}
Send3(key) {
    Send("3")
    ; Send("33333")
    ; Send(key)
}
Send4(key) {
    Send("4")
    ; Send("44444")
    ; Send(key)
}
and here's the data when it doesn't work. In this run - pressed Enter, k & j (same-ish time), Enter, k & j, Enter k & j


Output on system:

Code: Select all

3124
314
134
Note on the 2nd and third lines from above, the script did not catch the Up (as "h") at all for the j on one and the k on the other

Code: Select all

0D  01C	 	d	0.36	Enter          	
0D  01C	 	u	0.11	Enter          	
4B  025	h	d	0.05	k              	
33  004	i	d	0.01	3              	
33  004	i	u	0.00	3              	
4A  024	h	d	0.00	j              	
31  002	i	d	0.00	1              	
31  002	i	u	0.00	1              	
4A  024	h	u	0.08	j              	
32  003	i	d	0.00	2              	
32  003	i	u	0.00	2              	
4B  025	h	u	0.01	k              	
34  005	i	d	0.00	4              	
34  005	i	u	0.00	4              	
0D  01C	 	d	0.38	Enter          	
0D  01C	 	u	0.11	Enter          	
4B  025	h	d	0.03	k              	
33  004	i	d	0.00	3              	
33  004	i	u	0.00	3              	
4A  024	h	d	0.01	j              	
31  002	i	d	0.00	1              	
31  002	i	u	0.00	1              	
4B  025	h	u	0.09	k              	
34  005	i	d	0.00	4              	
34  005	i	u	0.00	4              	
0D  01C	 	d	0.30	Enter          	
0D  01C	 	u	0.12	Enter          	
4A  024	h	d	0.05	j              	
4B  025	h	d	0.00	k              	
31  002	i	d	0.00	1              	
31  002	i	u	0.00	1              	
33  004	i	d	0.02	3              	
33  004	i	u	0.00	3              	
4B  025	h	u	0.05	k              	
34  005	i	d	0.00	4              	
34  005	i	u	0.00	4     
and the code lines, showing no recognition of the up on the keys

Code: Select all

047: Send("3")
050: }
037: Send("1") (0.02)
040: } (0.06)
042: Send("2") (0.01)
045: }
052: Send("4")
055: } (0.52)
047: Send("3") (0.01)
050: }
037: Send("1")
040: } (0.09)
052: Send("4")
055: } (0.47)
037: Send("1") (0.02)
040: }
047: Send("3")
050: } (0.05)
052: Send("4") (0.02)
055: } (35.62)
I've found that the Send functions with more data (commented sends in code) *seem* to fail more often, but not sure if that's true or useful.

I've consistently reproduced by playing 8k video at 2x speed in the background, or loading a website from a crappy news organization that has more video ads than news, but choose your poison. Also been able to do it with a single key down-up under load, but it is more rare.

synthmusic
Posts: 8
Joined: 13 Nov 2021, 01:54

Re: v2 keyboard hook sometimes not capturing key actions

Post by synthmusic » 27 Jul 2022, 20:15

ALSO, I updated to v2 beta 7, and it is still an issue. I installed v1 as well, and will NOT reproduce under the same conditions, no keys are dropped when loading v2 continues to miss keystrokes.

Here's the v1 script for reference

Code: Select all

#SingleInstance Force

hotkey  $*j , Send1,  On
hotkey  $*j Up , Send2,  On
hotkey  $*k , Send3,  On
hotkey  $*k Up , Send4,  On

Send1:
    Send  1
return

Send2:
    Send  2
return

Send3:
    Send  3
return

Send4:
    Send  4
return


lexikos
Posts: 9554
Joined: 30 Sep 2013, 04:07
Contact:

Re: v2 keyboard hook sometimes not capturing key actions

Post by lexikos » 27 Jul 2022, 23:06

If this was a bug, I would expect to see many more reports. It is unlikely. Both versions share basically the same code for hotkeys, the keyboard hook and Send.

Your v1 and v2 scripts are not equivalent. With v2, the default Send mode is SendInput. See if the v1 script still behaves with SendMode Input.

synthmusic
Posts: 8
Joined: 13 Nov 2021, 01:54

Re: v2 keyboard hook sometimes not capturing key actions

Post by synthmusic » 30 Jul 2022, 03:15

lexikos wrote:
27 Jul 2022, 23:06
If this was a bug, I would expect to see many more reports. It is unlikely.
Totally agree with this sentiment, I was hesitant to post, but not sure what to try next. I could absolutely have a unique problem in my environment, though to further confirm, I ran tests on another win10 laptop and was able to see the same behavior.

Thanks for the SendMode point, it turns out to be another good point of data.

I whittled it down to a simpler test. Map a key to another key, hold it down and see if the original key ever peeks through without getting captured.

SETUP:
3 scripts

Code: Select all

; v1 - v1.1.34.03 default SendMode
hotkey, $1, test
hotkey, *2, test
hotkey, 3, test
test:
    Send .
    return

Code: Select all

; v1 SendInput - v1.1.34.03 SendMode Input
SendMode Input
hotkey, $1, test
hotkey, *2, test
hotkey, 3, test
test:
    Send .
    return

Code: Select all

; v2 - v2.0-beta.7
hotkey("$1", test)
hotkey("*2", test)
hotkey("3", test)
test(key) {
    Send(".")
}
in all 3 the "hotkeys and their methods" shows
k-hook $1
k-hook *2
reg 3

TEST
hold down 1, 2, or 3 and observe text output.

RESULT:
v1 default SendMode
no matter the test mapping, holding any send key the window never sees the original key itself. 1, 2, or 3 always sends "."

v1 SendMode Input
the k-hook keys succumb to the reported behavior. Holding them down will eventually result in the mapped source key getting seen by the window instead of the mapping. The reg mapped key works as expected, though and might slow down, but never sees the source key.

v2
the k-hook keys succumb to the reported behavior. Holding them down will eventually result in the mapped source key getting seen by the window instead of the mapping. The reg key occasionally returns an error noted below.


Example, holding down 1 for a bit in v2 or v1 SendInput results:
...........................................................................................................1.111...............1.........1..................................................................................................1..................................

also, while holding the "reg" hotkey in v2 occasionally will result in the following, but not in either v1:

---------------------------
thmTestingHoldKeyV2.ahk
---------------------------
71 hotkeys have been received in the last 1797ms.

Do you want to continue?
(see A_MaxHotkeysPerInterval in the help file)
---------------------------
Yes No
---------------------------

I'm not sure what to try next, but I'm happy to try anything suggested.

lexikos
Posts: 9554
Joined: 30 Sep 2013, 04:07
Contact:

Re: v2 keyboard hook sometimes not capturing key actions

Post by lexikos » 30 Jul 2022, 21:46

If you are getting the A_MaxHotkeysPerInterval warning and you aren't sending the hotkey itself (Send 3), the simplest explanation is that your key-repeat delay is set so low that holding down the key causes the hotkey to fire too many times within a short period, triggering the warning. It may occur in v2 and not v1 because of timing differences, such as those caused by SetBatchLines (v1 defaults to sleeping for 10ms every 10ms, although this only occurs between commands). The warning is meant as a countermeasure for runaway loops. If the hotkey is legitimately being activated that frequently, you'll probably just want to adjust A_MaxHotkeysPerInterval and/or A_HotkeyInterval.

However, I am unable to reproduce any of the issues you mentioned using your v2 code, and I have key-repeat set to the fastest setting allowed by Control Panel (which may not be the fastest possible). I am running Windows 11 on a Core i7-10700.

Except in the documented cases where SendInput reverts to SendEvent mode, SendInput temporarily deactivates the keyboard hook so that the system will correctly buffer user input while the send is in progress. In theory, there is an incredibly small delay between when the SendInput (system function) call completes and when the keyboard hook is reinstalled. In practice, and apparently even when the user's input has been buffered during the send, this generally isn't an issue. Perhaps it depends on the characteristics of the system (e.g. speed and number of CPU cores), and what else is happening at the time.

The presence of other software with a global keyboard hook would certainly also have an impact, as in that case the user's input is not buffered (by the system) and therefore might be processed during the Send, both interfering with the Send and preventing the script from detecting it.

SendEvent mode does not deactivate the keyboard hook. If you are sending multiple events, they can be interspersed with the user's input regardless of whether a keyboard hook is installed. There is less chance of this being an issue when you are only sending one key. (But even SendEvent "." usually results in multiple events: key-down, key-up and potentially modifier keys up/down as needed.) The upside is that leaving the hook active means that it will not miss any events (unless the system decides to stop calling it).

synthmusic
Posts: 8
Joined: 13 Nov 2021, 01:54

Re: v2 keyboard hook sometimes not capturing key actions

Post by synthmusic » 02 Aug 2022, 13:44

OK, thanks much for the reply and detailed breakdown.

For posterity, I'm on a i7-7700hq, win 10 , older but not particularly slow. I had an inkling other software might play a role, it seems more pronounced when working on VS Code (node / V8), chrome (V8), node projects (...node), but still occasional otherwise. Perhaps someday I'll find some software running that's the issue and I'll share.

vmech
Posts: 352
Joined: 25 Aug 2019, 13:03

Re: v2 keyboard hook sometimes not capturing key actions

Post by vmech » 03 Aug 2022, 12:52

@synthmusic
Can you try your example scripts with DllCall('SetProcessPriorityBoost', 'Ptr', DllCall('GetCurrentProcess'), 'Int', True) command somewhere at the top of script ?
Please post your script code inside [code] ... [/code] block. Thank you.

synthmusic
Posts: 8
Joined: 13 Nov 2021, 01:54

Re: v2 keyboard hook sometimes not capturing key actions

Post by synthmusic » 10 Aug 2022, 22:23

@vmech
oooh, good idea! Thanks!

FWIW, that call did not appear to up the priority: in the Task Manager -> Details tab, the Base Priority still showed as Normal. After a bit of digging, though,

Code: Select all

DllCall('SetPriorityClass', 'Ptr', DllCall('GetCurrentProcess'), 'Int', 0x00000080)
got it running at High Priority.

It did seem to be a little bit less often, ish, maybe. :) I won't say it did nothing, but it also did not solve the problem. I even bumped it manually to realtime, but it is still happening.

vmech
Posts: 352
Joined: 25 Aug 2019, 13:03

Re: v2 keyboard hook sometimes not capturing key actions

Post by vmech » 12 Aug 2022, 02:21

synthmusic wrote:
10 Aug 2022, 22:23
@vmech
oooh, good idea! Thanks!

FWIW, that call did not appear to up the priority: in the Task Manager -> Details tab, the Base Priority still showed as Normal. After a bit of digging, though,

Code: Select all

DllCall('SetPriorityClass', 'Ptr', DllCall('GetCurrentProcess'), 'Int', 0x00000080)
got it running at High Priority.

It did seem to be a little bit less often, ish, maybe. :) I won't say it did nothing, but it also did not solve the problem. I even bumped it manually to realtime, but it is still happening.
You can't see dynamic priority boosting in any process monitoring programs. Dynamic process/thread priority enabled by default for all processes (at least for user-space processes). More about.

Also you can change autohotkey process base priority by using ProcessSetPriority function.
Please post your script code inside [code] ... [/code] block. Thank you.

synthmusic
Posts: 8
Joined: 13 Nov 2021, 01:54

Re: v2 keyboard hook sometimes not capturing key actions

Post by synthmusic » 13 Aug 2022, 20:42

@vmech

read the link, and realized my missed understanding, a) dynamic boost, and b) it appears you are turning off dynamic boost, curious to understand what this is testing / will tell us?

vmech
Posts: 352
Joined: 25 Aug 2019, 13:03

Re: v2 keyboard hook sometimes not capturing key actions

Post by vmech » 15 Aug 2022, 05:49

@synthmusic
Just also trying to figure out what causes this strange behavior when recognizing keypresses in ahk v2.
Please post your script code inside [code] ... [/code] block. Thank you.

Post Reply

Return to “Bug Reports”