Single and Long Press CapsLock

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
reborn
Posts: 10
Joined: 23 Jun 2019, 06:44

Single and Long Press CapsLock

Post by reborn » 07 Dec 2022, 17:42

Hello everyone!
I want give my CapsLock key more functionality:
1.when single-pressed,CapsLock works as usual;
2.when long-pressed,switch input language and keyboard layout.(win+space);
3.when held down and in combination with IJKL it will function as arrow keys.

So I copy and modified two scripts from viewtopic.php?t=88662 and https://stackoverflow.com/questions/57311941/make-capslock-work-properly-when-single-double-and-combo-pressed.

Code: Select all

CapsLock::
KeyWait, %A_ThisHotkey%, T.25
if held := ErrorLevel
    ;SoundBeep, 1000, 120
KeyWait, %A_ThisHotkey%
Send % held ? "#{Space}" : ""
SetTimer, RestoreCapsLockState, 50
Return

~Capslock Up:: Return

CapsLock & J::Send {Left}
CapsLock & K::Send {Down}
CapsLock & L::Send {Right}
CapsLock & I::Send {Up}

RestoreCapslockState:   
    KeyWait, CapsLock ; wait for Capslock to be released
    SetTimer, RestoreCapsLockState, Off
    If (A_PriorKey != "CapsLock") 
        SetCapsLockState % !GetKeyState("CapsLock", "T") ; Toggles CapsLock to its opposite state, requires [v1.1.30+]
Return
It works,but with little inconvenience:
when I long-press CapsLock,it will send #{Space} but also CapsLock,so whenever I long-press CapsLock it will set my CapsLock state oppositely.When CapsLock is off,I long press CapsLock,my input language changed and CapsLock is on,but I want CapsLock stay the same.
I just want when I long press CapsLock it will send win+space but CapsLock state stay the same as before,can anyone help?


coldfix
Posts: 1
Joined: 25 Nov 2023, 09:00

Re: Single and Long Press CapsLock

Post by coldfix » 25 Nov 2023, 09:19

Press Capslock = switch layout. Hold Capslock for 0.3 sec = enable/disable Capslock.

Code: Select all

CapsLock::
    KeyWait, CapsLock, T0.3
    if (ErrorLevel)
    {
        SetCapsLockState, % !GetKeyState("CapsLock", "T")
    }
    else
    {
        Send #{Space}
    }
return
I faced win+? combinations accidentally working so now I use caps to switch layouts with this code:

Code: Select all

CapsLock::
    KeyWait, CapsLock, T0.3
    if (ErrorLevel)
    {
        SetCapsLockState, % !GetKeyState("CapsLock", "T")
    }
    else
    {
        Send, {LAlt down}{LShift down}{LShift up}{LAlt up}
    }
return

Post Reply

Return to “Ask for Help (v1)”