Keybd hook breaks with AHK 32-bit

Report problems with documented functionality
2_05
Posts: 21
Joined: 09 May 2018, 10:28

Keybd hook breaks with AHK 32-bit

15 Nov 2020, 06:20

I've been using AHK 32-bit as I am hooking it with a 32-bit dll. After migrating to Windows 10 I've noticed that hotkeys that require the keybd hook stop working after some time. Also Suspend stops working. After a lot of testing I discovered that this happens when other applications slow the processing of keyboard input.

I've been able to replicate the issue with the code below (32-bit) and the use of a third party application that slows the processing of keyboard input.
  1. Compile the keydelayer script as 64-bit ahk named keydelayer (64-bit).exe
  2. Run keydelayer (64-bit).exe first!
  3. Run the demoscript as 32-bit ahk
  4. Press ^k. Notice in the KeyHistory that the keybd hook is on.
  5. Press ^s to suspend. Notice in the KeyHistory that the keybd hook is off.
  6. Use the tray menu to set Suspend Off.
  7. Pressing q repeatedly will break the Keybd hook and hotkeys (q, r, e) stop working. Even with the use of #IfTimeout 50
  8. Pressing w repeatedly will break the Keybd hook and hotkeys (q, r, e) stop working, even though no hotkey is defined for w in the demoscript.
  9. Press ^s after the Keybd hook is broken. Notice in the KeyHistory that the keybd hook is still on (This hotkey still works as it does not use the Keybd hook).
  10. Use the tray menu to set Suspend Off after the previous step. Hotkeys are still not working.
  11. Pressing r repeatedly does not break the keybd hook.
  12. Pressing t (Send, ^c) after the Keybd hook is broken causes a stuck Ctrl key.
Also check the related analysis of Suspend by @swagfag https://www.autohotkey.com/boards/viewtopic.php?p=358689#p358689.

I hope this is something that can be fixed. Also please look into Suspend to see if this can be fixed.

Demoscript. Run this demoscript after starting the compiled keydelayer script below.

Code: Select all

#SingleInstance, Force
#IfTimeout 50

; Run this script as 32-bit. Run keydelayer script as 64-bit by compiling it first.
; Pressing either q or w repeatedly will break the keyboard hook
;   of this script even though there are no delays in this script itself.
;   Use of #IfTimeout does not prevent this
; Also notice how the keys q and w are not slowed down by the keydelayer untill
;   the keybdhook breaks.

If(!A_Is64bitOS or A_PtrSize > 4)
	MsgBox, Issue can only be demonstrated on 64-bit OS with 32-bit AHK.

Process, Exist, keydelayer (64-bit).exe
if(!ErrorLevel)
{
	MsgBox, Start 64-bit keydelayer first
	ExitApp
}	

#If winex("Not Existing Window")
	*q::Return ; never reached
	*r::Return ; never reached
#If

*e::Outputdebug, Myscript still working

t::Send, ^c ; Pressing T after Keybdhook was broken causes a stuck Ctrl key.

^s::
	Suspend, On
	KeyHistory	
	return
	
^k::KeyHistory

Esc::Exitapp

winex(win)
{
	we := WinExist(win)
	OutputDebug, Myscript eval
	return we
}
Keydelayer script. To mimic a third party application that slows keyboard input, I use the following script. Compile it and name it keydelayer (64-bit).exe

Code: Select all

; This script simulates a third party application that slows keyresponse.

#SingleInstance, Force
; IfTimeout not included on purpose. 

If(A_PtrSize < 8)
	MsgBox, Works best when compiled as 64-bit AHK.

#If delay()
	q::return
	w::return
#If

delay()
{
	Outputdebug, Keydelayer eval
	sleep, 500 ; Delay for demonstration purspose
	return false
}
Last edited by 2_05 on 28 Nov 2020, 16:11, edited 5 times in total.
User avatar
nnnik
Posts: 4479
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Keybd hook breaks with AHK 32-bit

20 Nov 2020, 05:03

I do not think this is caused or fixable from AHKs side.
Recommends AHK Studio
2_05
Posts: 21
Joined: 09 May 2018, 10:28

Re: Keybd hook breaks with AHK 32-bit

28 Nov 2020, 16:08

If it is caused by Windows 10 it may not be preventable from AHKs side, though I cannot understand why Windows 10 would by design cancel the Keybd hook when AHK is not actually the source of the delay. And why it does so only for 32-bit software but not for 64-bit software. Is it a Windows 10 bug?

Aside from being able to prevent it, I think there are a few things that can be fixed to prevent unwanted side effects.
  • If I understand @swagfag's post that I linked to in the TS correctly, the Suspend function is not correctly handling the broken Keybdhook. If this can be fixed the user can more easily refresh the Keybd hook to recover from the broken Keybd hook, and a restart of the script can be prevented.
  • If there is some command to test the Keybd hook, the user can include a script to periodically check the hook.
  • There are unwanted side affects that should be prevented, such as a stuck Ctrl key when using Send, ^c after the Keybd hook has broken (I just included this in the TS as well).

Return to “Bug Reports”

Who is online

Users browsing this forum: No registered users and 2 guests