Toggle NumLock ScrollLock & CapsLock Issue

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
OrangeCat
Posts: 65
Joined: 14 Jun 2022, 00:47

Toggle NumLock ScrollLock & CapsLock Issue

Post by OrangeCat » 01 May 2024, 16:03

I was provided a CapsLock toggle answer that works perfect for CapsLock
I tried to replicate the same for NumLock and ScrollLock but they don't toggle at all.
What is it that I'm doing wrong?

Code: Select all

    TOGGLE_CapsLock:
        If GetKeyState("CapsLock", "T") = 1
            SetCapsLockState, Off
        Else If GetKeyState("CapsLock", "F") = 0
            SetCapsLockState, On
        Return
    RETURN

    TOGGLE_NumLock:
        If GetKeyState("NumLock", "T") = 1
            SetNumLockState, Off
        Else If GetKeyState("NumLock", "F") = 0
            SetNumLockState, On
        Return
    RETURN

    TOGGLE_ScrollLock:
        If GetKeyState("ScrollLock", "T") = 1
            SetScrollLockState, Off
        Else If GetKeyState("ScrollLock", "F") = 0
            SetScrollLockState, On
        Return
    RETURN
    
    ; ============================================================================
    
    Shift & CapsLock::Gosub, TOGGLE_CapsLock ; <------- works
    Shift & NumLock::Gosub, TOGGLE_NumLock ; <--------- doesn't work
    Shift & ScrollLock::Gosub, TOGGLE_ScrollLock ; <--- doesn't work

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

Re: Toggle NumLock ScrollLock & CapsLock Issue

Post by mikeyww » 01 May 2024, 17:49

Hello,

I would read the "Mode" section on the following page.

https://www.autohotkey.com/docs/v1/lib/GetKeyState.htm#Function_Parameters

Rohwedder
Posts: 7895
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Toggle NumLock ScrollLock & CapsLock Issue

Post by Rohwedder » 02 May 2024, 00:22

Hallo,
GetKeyState has no “F” mode! Try:

Code: Select all

    TOGGLE_CapsLock:
        If GetKeyState("CapsLock", "T") = 1
            SetCapsLockState, Off
        Else If GetKeyState("CapsLock", "T") = 0
            SetCapsLockState, On
        Return
    RETURN

    TOGGLE_NumLock:
        If GetKeyState("NumLock", "T") = 1
            SetNumLockState, Off
        Else If GetKeyState("NumLock", "T") = 0
            SetNumLockState, On
        Return
    RETURN

    TOGGLE_ScrollLock:
        If GetKeyState("ScrollLock", "T") = 1
            SetScrollLockState, Off
        Else If GetKeyState("ScrollLock", "T") = 0
            SetScrollLockState, On
        Return
    RETURN
    
    ; ============================================================================
    
    Shift & CapsLock::Gosub, TOGGLE_CapsLock ; <------- works
    Shift & NumLock::Gosub, TOGGLE_NumLock ; <--------- works
    Shift & ScrollLock::Gosub, TOGGLE_ScrollLock ; <--- works
or:

Code: Select all

+CapsLock::SetCapsLockState,% !GetKeyState("CapsLock", "T") ; <--------- works
+NumLock::SetNumLockState,% !GetKeyState("NumLock", "T") ; <------------ works
+ScrollLock::SetScrollLockState,% !GetKeyState("ScrollLock", "T") ; <--- works

Post Reply

Return to “Ask for Help (v1)”