MouseHook causes severe input issues for hotkeys which do not use it

Get help with using AutoHotkey and its commands and hotkeys
katopuh
Posts: 6
Joined: 19 Mar 2019, 08:49

MouseHook causes severe input issues for hotkeys which do not use it

03 May 2019, 07:48

We have been having severe mouse and keyboard input issues with hotkeys especially those using `KeyWait`. We found that removing every application-specific `#If` from the code minimises if not eliminates the issue.

After further debugging, we determined the direct cause to be MouseHook. When adding `#InstallMouseHook` (or a hotkey which calls for it) to our simple test script below, we can replicate the problem instantly:

Code: Select all

#If WinActive("ahk_exe status.exe")
j::
  Send, {j}
  KeyWait, j
  Send, {j}
Return
Compiling the code using Compile_AHK rather than the default compiler also minimises (but not eliminates) the issue. Running the script bare does work fine, but it would require deploying AutoHotkey to endpoints which is undesirable.
katopuh
Posts: 6
Joined: 19 Mar 2019, 08:49

Re: MouseHook causes severe input issues for hotkeys which do not use it

03 May 2019, 07:52

Running the test script above with `#InstallMouseHook` almost instantly locks up mouse and keyboard input on our ThinkPads lasting until seconds after releasing j.
User avatar
Nextron
Posts: 1354
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win7 x64 AHK: Unicode x32

Re: MouseHook causes severe input issues for hotkeys which do not use it

03 May 2019, 11:52

So it also happens on uncompiled scripts as well? Does it make a difference if you use #IfWinActive instead?

Mod edit: Moved to 'Ask for help' until confirmed as bug.
User avatar
Nextron
Posts: 1354
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win7 x64 AHK: Unicode x32

Re: MouseHook causes severe input issues for hotkeys which do not use it

03 May 2019, 12:10

#If's already makes the associated hotkeys hooked variants.
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: MouseHook causes severe input issues for hotkeys which do not use it

03 May 2019, 16:52

I recently had an issue with the mouse hook and hotkeys when SendMode was set to Input (or when I used SendInput instead of Send). I would try adding SendMode Event to the top of the script, especially in place of any other SendMode command, or changing your Sends to SendEvent. It may well do nothing for you, but it's something quick to try, anyways.
User avatar
Nextron
Posts: 1354
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win7 x64 AHK: Unicode x32

Re: MouseHook causes severe input issues for hotkeys which do not use it

04 May 2019, 03:17

What kind of issue? During SendInput the hook would be temporarily disabled, opposed to during SendEvent. Also, when running multiple scripts using hooks, SendInput would revert to SendEvent using lower wait times. Perhaps that explains the 'issue'?
User avatar
evilC
Posts: 4688
Joined: 27 Feb 2014, 12:30

Re: MouseHook causes severe input issues for hotkeys which do not use it

04 May 2019, 10:10

FYI doing this is asking for trouble

Code: Select all

#If WinActive("ahk_exe status.exe")
$j::
  Send, {j}
  KeyWait, j
  Send, {j}
Return
If you have more than one hotkey with a pattern like this, then the script will misbehave whenever you hold more than one of these hotkeys, because of how AHK threading works.

Instead, use

Code: Select all

#If WinActive("ahk_exe status.exe")
$j::
  Send, {j}
Return

$j up::
  Send, {j}
Return
For an extra line or two of code, you get the same effect, and it doesn't suffer from this issue

For more information, see https://www.autohotkey.com/boards/viewtopic.php?t=19745
swagfag
Posts: 2484
Joined: 11 Jan 2017, 17:59

Re: MouseHook causes severe input issues for hotkeys which do not use it

04 May 2019, 10:25

these codes arent equivalent but yeah generally ure better off not using Keywait
katopuh
Posts: 6
Joined: 19 Mar 2019, 08:49

Re: MouseHook causes severe input issues for hotkeys which do not use it

11 May 2019, 23:58

Sorry for my delayed reply. We have put everything MouseHook into a separate thread with AHK_H as somewhat of a solution now.

@gregster Thanks, but the hotkeys should already be hook-dependent (according to the docs). Prepending `$` did not resolve the MouseHook issue. We have tried!

@Osprey The default SendMode appears to be SendEvent (according to the docs), but we have tried SendInput regardless which did not help in any way. Thanks!

@evilC Unfortunately, we do require KeyWait. The keys are never pressed simultaneously, but we will implement your advice where possible. Thanks!

We found the longer you hold the key, the more it affects the system’s stability. Both keyboard and mouse input are severely delayed until they stop responding altogether. There is something dodgy going on as soon as MouseHook is called despite it not being used. We are guessing it is supposed to degrade performance (apparently, to a rather noticeable degree on some systems), so the real issue we have been having is that it also affects hotkeys which do not use MouseHook at all.

We may still carry out further testing, but we are satisfied with the results for now.
User avatar
evilC
Posts: 4688
Joined: 27 Feb 2014, 12:30

Re: MouseHook causes severe input issues for hotkeys which do not use it

28 May 2019, 11:22

In all my years of coding AHK, I have never, ever seen ANY code that uses KeyWait which cannot be done in a more robust manner. A short timeout is not bad, but a KeyWait with no timeout is bad code, period IMHO

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], flyingDman, PipeDreams and 167 guests