Hotkey stops firing after another hotkey is executed

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Randy31416
Posts: 58
Joined: 15 Jan 2014, 19:09

Hotkey stops firing after another hotkey is executed

Post by Randy31416 » 04 Jul 2022, 07:45

The code below is winnowed and simplified from a large program that does not work on recent releases of AHK. The large program works on V1.33.02 but not on more recent releases. This winnowed sample does not work on either the old or the new version.

The program contains a hotkey !+I that overloads a hotkey in the Windows start menu. This works fine, firing its code, until certain other hotkeys are executed. In the sample below, the other hotkey is NumPad5 or NumPadClear (depending on the state of NumLock). When that other hotkey is pressed, it fires, but thereafter !+I does not fire, and the !+I keypress instead starts the program assigned to the start menu shortcut (explorer.exe for testing).

So, on a fresh load of this program, pressing !+I, then ^!NumPad5, and then !+I will fire !+I the first time but not the second. ^!NumPad5 continues to fire, and so do !+H (which does not overload a hotkey in the Windows start menu) and ^!Q.

A couple of remarks. If the !+I hotkey is replaced by, say, ^!I then the problem does not arise: I think the shift is involved in this somehow. Similarly, if the sendinput, ^c is removed from the ^!NumPad5 hotkey code, then the problem disappears. If the GetKeyState functions are removed from the low-level keyboard hook the problem disappears (note that shift is queried by these functions). Finally, if the low-level keyboard hook is removed the problem also disappears.

One other oddity. On a fresh load of the program, pressing Ctrl and Alt and then repeatedly pressing NumPad5 causes the NumPad5 to fire on each keypress of NumPad5. If all three keys are released and then this sequence is repeated (press and hold Ctrl and Alt and then repeatedly press NumPad5), then NumPad5 fires only on the first keypress of NumPad5.

This is all runing under Win 10 Pro 64-bit (up to date).

It seems to me that the !+I hotkey should continue to fire and that NumPad5 should fire repeatedly. Is this a bug in my code? A bug in AHK? Why do the hotkeys stop firing? Enlightenment, please.

Code: Select all

    Global WH_KEYBOARD_LL = 0xD
    Global RunHotBoardWindowHandle := 0
    Menu, Tray, MainWindow
    RunHotBoardBegin()
    Return

    !+H:: SoundBeep, 555, 111   ;- this hotkey does not override a Windows start-menu hotkey
    !+I:: SoundBeep, 555, 111   ;- this hotkey overrides a Windows start-menu hotkey (which for testing starts Explorer)

    ^!NumPad5::                 ;- this fires if NumLock is on but it also stops the !+I hotkey from working
      SoundBeep, 333, 111
      SendInput, ^c
      Return
    ^!NumPadClear::             ;- this fires if NumLock is off but it also stops the !+I hotkey from working
      SoundBeep, 777, 111
      SendInput, ^c
      Return

    ^!Q:: ExitApp

  RunHotBoardBegin( )
  {
    Global RunHotBoardWindowHandle
    RunHotBoardWindowHandle := DllCall("SetWindowsHookEx", "Int", WH_KEYBOARD_LL, "UInt", RegisterCallback("RunHotBoardCallBack"), "UInt", 0, "UInt", 0, "Ptr")
  }
  RunHotBoardCallBack( nCode , wParam , lParam )
  {
    Critical
    Global RunHotBoardWindowHandle
    NowFlip := GetKeyState("Shift", "P") ^ GetKeyState("CapsLock", "T")
    Return, DllCall("CallNextHookEx", "Ptr", RunHotBoardWindowHandle, "Int", nCode, "Ptr", wParam, "Ptr", lParam)
  }

Return to “Ask for Help (v1)”