Search code examples
autohotkey

A_ThisHotkey and A_PriorHotkey only take into account keys declared as hotkeys


I am trying to write a script that upon release of Ctrl, it will tell me if it was the most recently pressed key or if another key was pressed since Ctrl was down.

So if F was pressed while Ctrl was down, I need to know. The "while holding down" key should be any key not just a modifier like Ctrl.

I wrote the following, but I am having a peculiar issue with it. It seems it will only inform me of a key being pressed, IF the key itself is declared as a hotkey in the script.

So with the following script, if F was pressed, I will be informed but as soon as I comment out the line ^f:: ; Do Nothing, I will not be informed if F is pressed:

#singleinstance, force
#Persistent
#NoEnv
#Warn, All, OutputDebug

~LCtrl::
ToolTip, % "Most Recent Key: " A_ThisHotkey "`n Prior Key: " A_PriorHotkey   
Return
^f::  ; Do Nothing

With this in mind, I would have to declare every keyboard and mouse key on my system to get my program to work. This is not ideal and it will simply not work in my main script.

Is there a way to populate A_ThisHotkey and A_PriorHotkey with keyboard/Mouse Keys that are not declared as Hotkeys in the script?

Thanks for any help.


Solution

  • Hotkeys in AHK perform tasks and have to be defined by the user. Therefore A_ThisHotkey and A_PriorHotkey allways refer to hotkeys already defined in the script.

    To get the name of the last pressed keyboard/mouse key use A_PriorKey:

    #NoEnv
    #singleinstance, force
    #Warn, All, OutputDebug
    #InstallKeybdHook
    #InstallMouseHook
    
    ~LCtrl:: ToolTip, % "Most Recent Key: " A_ThisHotkey "`n Prior Key: " A_PriorKey