DataMig
DataMig

Reputation: 11

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.

Upvotes: 0

Views: 822

Answers (2)

DataMig
DataMig

Reputation: 11

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 >

Upvotes: 1

PGilm
PGilm

Reputation: 2312

Do you mean the Pause key? Or suspend as in suspending the hotkey macro?

Try:

CapsLock::
    Send {Pause}
    ToggleCapslock()
Return

There is good help here on Pause:

https://www.autohotkey.com/docs/KeyList.htm#other

Pause Pause or Ctrl+NumLock. While Ctrl is held down, Pause produces the key code of CtrlBreak and NumLock produces Pause, so use ^CtrlBreak in hotkeys instead of ^Pause.

Suspending the hotkeys is here: https://www.autohotkey.com/docs/commands/Suspend.htm

Suspend Disables or enables all or selected hotkeys and hotstrings.

Let us know what you want,

Upvotes: 0

Related Questions