toggleautohotkeysuspendcapslock

AHK script: pressing CapsLock to toggle CapsLock + Suspend


Setting up an Autohotkey script.

How to make CapsLock key do 'Suspend' while toggling the CapsLock state on the same key press?

I want this:

CapsLock::
Suspend
ToggleCapslock()
Return

The code should be able to make CapsLock key toggle both the CapsState and the Suspend state with a single key press of CapsLock.


How to achieve that?

The script below doesn't toggle both the CapsLock state and the Suspend state.

1st key press: it activates Suspend and sets CapsLockState to OFF.

2nd key press: it does unsuspend, CapsLockState remains at OFF.

CapsLock::
Suspend
;ToggleCapslock()
if GetKeyState("CapsLock", "P")
    SetCapsLockState, Off
if !GetKeyState("CapsLock", "P")
    SetCapsLockState, On
return

ToggleCapslock() {
    flag := false
        if (flag) {
            SetCapsLockState, On
        } else {
            SetCapsLockState, Off
        }
        flag := !flag
}

I want to toggle Suspend/Unsuspend AND CapsLockState ON/OFF on each single press of CapsLock.

(Essentially, this thread asks how to put actions to CapsLock key while maintaining its native function.)

Glad for your help.


Solution

  • Final easy Solution:

    ~CapsLock::Suspend
    

    The documentation covers the most fundamental solution for this problem you could possible come up with. (Found by reading through the documentation: https://www.autohotkey.com/docs/Hotkeys.htm#Symbols)

    ~ : When the hotkey fires, its key's native function will not be blocked (hidden from the system).

    < closeThread >