Repeated hotkeys not working remotely with KeyboardHook

Report problems with documented functionality
User avatar
catonthetree
Posts: 1
Joined: 20 May 2020, 10:40

Repeated hotkeys not working remotely with KeyboardHook

20 May 2020, 12:12

Hi All,

I'm using AHK 1.1.32.00 on Win 10 and I'm trying to make the system behave as close to macOS as I can get it. I'm using it both with a physical Macally keyboard and via Chrome Remote Desktop (CRD) from a Mac (latest version of Win, macOS, Chrome). Both keyboards have the same modifier keys (Fn, Ctrl, Alt/Option, Win/Cmd). I swapped Alt and Win in the Windows registry and in the CRD connection dialog. Then I'm binding several Alt Hotkeys to their corresponding Ctrl hotkeys (e.g. !t and !w should map to ^t and ^w to open and close new browser tabs, same for x/c/v etc. There are many more but I minimized the example for my question).

Code: Select all

!t::SendInput ^t
!w::SendInput ^w
This works as expected on the physical keyboard and from the remote desktop connection. In particular I can hold down Alt and hit t multiple times to open multiple tabs.

However, when adding any hotkey that installs the keyboard hook, repeatedly triggering a hotkey while holding down the modifier stops working. Instead it just starts typing the letter. Instead of two new tabs I get one tab with one letter 't' in it.

Code: Select all

#Backspace::SendInput ^{Backspace}
I confirmed that #InstallKeybdHook without adding any other hotkeys also breaks the repeated hotkeys for the remote connection.

Without the hook installed, the traces looks like this (On the remote machine, physically holding down Alt, hitting t twice, letting go of Alt):

Code: Select all

VK  SC	Type	Up/Dn	Elapsed	Key		Window
-------------------------------------------------------------------------------------------------------------
A2  01D	i	d	8.45	LControl       	New Tab - Google Chrome
A4  038	i	u	0.00	LAlt           	
54  014	i	d	0.00	t              	
54  014	i	u	0.00	t              	
A4  038	i	d	0.00	LAlt           	
A2  01D	i	u	0.00	LControl       	
A2  01D	i	d	1.28	LControl       	
A4  038	i	u	0.00	LAlt           	
54  014	i	d	0.00	t              	
54  014	i	u	0.00	t              	
A4  038	i	d	0.00	LAlt           	
A2  01D	i	u	0.00	LControl
When the keyboard hook is installed, it looks like this:

Code: Select all

VK  SC	Type	Up/Dn	Elapsed	Key		Window
-------------------------------------------------------------------------------------------------------------
A4  038	a	d	6.95	LAlt           	New Tab - Google Chrome
54  014	a	d	1.00	t              <-- pressing 't' for the first time
54  014	a	u	0.00	t              	
A2  01D	i	d	0.00	LControl       	
A4  038	i	u	0.00	LAlt           	
54  014	i	d	0.00	t              	
54  014	i	u	0.00	t              	
A2  01D	i	u	0.00	LControl       	
54  014	a	d	1.31	t              	<-- pressing 't' for the second time
54  014	a	u	0.00	t              	
A4  038	a	u	1.09	LAlt           	
You can see that the second time t is pressed, it does not seems to trigger the hotkey again.

In both cases the full hotkey map looks like this:

Code: Select all

Type	Off?	Level	Running	Name
-------------------------------------------------------------------
reg				#+k
reg				#+r
reg				!t
reg				!w
Only the remote keyboard is affected. Even with the keyboard hook installed the local attached keyboard continues to work as expected.

So the question here is: Is this a bug? Why does the keyboard hook merely being installed mess with any regular (non-keyboard-hook) hotkeys are handled?

I also found that rewriting the hotkeys like so makes them work repeatedly:

Code: Select all

!t::SendInput {Blind}{LAlt Up}{LCtrl Down}{t}{LCtrl Up}{LAlt Down}
However, once such a hotkey has been triggered, all other hotkeys that are not re-written in this style stop working, so it's not a very practical workaround either.

Thanks for any help!
lexikos
Posts: 6879
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Repeated hotkeys not working remotely with KeyboardHook

24 May 2020, 00:44

The keyboard hook is used to track the modifier key states so that Send knows whether the key should be up or down when it finishes sending keys. If the hook is not installed, Send guesses which state it should be in based on #HotkeyModifierTimeout and which modifiers were used to trigger the hotkey.

Without the hook, Send guesses that you are still physically holding Alt, so puts it back into the down state (sends Alt-down). When you press T a second time, Alt is logically down, so the system generates an Alt-T hotkey message.

Your second KeyHistory shows that Alt was released and not pressed down again before the second T, so the system is not seeing Alt-T, just T without Alt. This is most likely because when you pressed Alt the first time, it was registered as an artificial keystroke (presumably having been sent by CRD), not a physical keystroke. In other words, the physical state of Alt is not pressed, so Send does not press Alt back down.

In most cases, if the key is not physically down, Send automatically "restoring" it to the pressed state would cause it to act as though it is stuck down, until you press and release it again. That is obviously to be avoided.

So in other words, this problem is by design.

I think if you add #UseHook or the $ prefix, the hotkeys will work. Registered hotkeys (i.e. the system) depend on the logical modifier state, whereas hook hotkeys depend on the hook's own tracking of the "logical" state, ignoring any implicit changes that scripts make, such as the Alt-up in your KeyHistory.
joefiesta
Posts: 377
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

Re: Repeated hotkeys not working remotely with KeyboardHook

26 May 2020, 07:44

@lexicos:

To say that a problem is by design, but not point to documentation about it seems to indicate the documentation is lacking.
lexikos
Posts: 6879
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Repeated hotkeys not working remotely with KeyboardHook

27 May 2020, 03:58

It could be documented for all I know. I didn't design it, I didn't write the majority of the relevant documentation, and neither did lexicos.

Return to “Bug Reports”

Who is online

Users browsing this forum: No registered users and 11 guests