Page 1 of 1

Control hanged out unpredictively

Posted: 17 Jan 2022, 14:52
by Totoro83
Hi, I'm very new to AutoHotKey, so I hope this is not a silly question.

I implemented a simple script that change the behavior of caps lock.
My intended behavior is that if caps lock is pressed in some specific windows it is reinterpreted as esc if pressed alone, as control if pressed with other keys (but not control), and have the normal behavior if pressed with control.

I implemented this script as follows:

Code: Select all

Processes := ["Code.exe"]
Classes := []

#SingleInstance Force
#NoEnv
#Warn

#if !GetKeyState("Control") && (IsSpecialProcess() || IsSpecialClass())
*CapsLock::
    Send {LControl Down}
    KeyWait CapsLock
    Send {LControl Up}
    if (A_PriorKey = "CapsLock")
        Send {Esc}

IsSpecialProcess() {
    global Processes
    WinGet process, ProcessName, A
    return IsIn(process, Processes)
}

IsSpecialClass() {
    global Classes
    WinGetClass class, A
    return IsIn(class, Classes)
}

IsIn(value, collection) {
    for k, v in collection
        if (v = value)
            return 1
    return 0
}
Notice as control is sent even when esc is sent, my rationale is that an additional control pressed before the esc should not be a problem.
Well, it works great most of the time, but occasionally control simply get stuck, like if it was still pressed, and I have to press control again to unlock it. How can I solve?

Re: Control hanged out unpredictively

Posted: 22 Jan 2022, 14:02
by mikeyww
After the problem occurs, examine the KeyHistory to understand the sequence of keys. Multi-line hotkey routines generally end in Return.