Detect if a key has been left pressed inside a script

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Detect if a key has been left pressed inside a script

Post by wetware05 » 28 May 2023, 13:33

Hi.

The title explains it all. But I will make myself understood. If someone has created a big script out of little scripts, and they did it when they were a newbie, if in any of their scripts a key is held down (sometimes triggered by pressing two keys at the same time), what causes the computer becomes erratic, in such a way that the only way to resolve the situation is to restart the computer... :evil: is there any routine or way to know that a key has been pressed and which key it is, by reversing its pressing? Going through all the scripts becomes infeasible and tedious (but I know I'll have to do it sometime).

Rohwedder
Posts: 7622
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Detect if a key has been left pressed inside a script

Post by Rohwedder » 29 May 2023, 02:32

Hallo,
try:

Code: Select all

*q::Release_Keys()
Release_Keys()
{ ;release all pressed keys
    Loop, 0xFF
        IF GetKeyState(Key:=Format("VK{:X}",A_Index))
            SendInput, {%Key% up}
}
And try this. All currently pressed keys and buttons are displayed. A mouse wheel rotation saves it to the clipboard.

Code: Select all

; Send, {q Down}{w Down}

SetTimer, KeyCombination, 50,% ClipBoard := ""
KeyCombination:
IF A_TimeIdlePhysical > 100
    Return
KeyCombination := KeyCombination()
ToolTip,% KeyCombination?KeyCombination:"ClipBoard:`n" ClipBoard
Return
*WheelUp::
*WheelDown::ClipBoard := KeyCombination 
KeyCombination(ExcludeKeys:="")
{ ;All pressed keys and buttons will be listed
    ExcludeKeys .= "{Shift}{Control}{Alt}{WheelUp}{WheelDown}"
    Loop, 0xFF
    {
        IF !GetKeyState(Key:=Format("VK{:02X}",0x100-A_Index))
            Continue
        If !InStr(ExcludeKeys,Key:="{" GetKeyName(Key) "}")
            KeyCombination .= RegexReplace(Key,"Numpad(\D+)","$1")
    }
    Return, KeyCombination
}

wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Detect if a key has been left pressed inside a script

Post by wetware05 » 29 May 2023, 04:49

Thank you very much @Rohwedder. :thumbup:

I have tried the second one and it works perfectly. I will have the first one as an emergency kit for when it happens again.

Post Reply

Return to “Ask for Help (v1)”