Control hanged out unpredictively

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Totoro83
Posts: 1
Joined: 17 Jan 2022, 14:35

Control hanged out unpredictively

Post by Totoro83 » 17 Jan 2022, 14:52

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?

User avatar
mikeyww
Posts: 26598
Joined: 09 Sep 2014, 18:38

Re: Control hanged out unpredictively

Post by mikeyww » 22 Jan 2022, 14:02

After the problem occurs, examine the KeyHistory to understand the sequence of keys. Multi-line hotkey routines generally end in Return.

Post Reply

Return to “Ask for Help (v1)”