Problems with SetCapsLockState, AlwaysOff when using capslock as a modifier key

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Florian
Posts: 30
Joined: 05 Dec 2019, 09:05

Problems with SetCapsLockState, AlwaysOff when using capslock as a modifier key

26 Dec 2019, 07:27

In my scripts, I often use capslock as a modifier key like this:

Code: Select all

#if getkeystate("capslock", "p")
^a:: ;capslock + Ctrl + A
But when I add

Code: Select all

SetCapsLockState, AlwaysOff
in my scripts, only the last script that I started (and that contains this code) actually works. If I don't put that in there, all scripts work but capslock toggles on and off. I want to disable capslock but use it as a modifier key. Any idea why that happens and how to solve this?
User avatar
Nextron
Posts: 1391
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: Problems with SetCapsLockState, AlwaysOff when using capslock as a modifier key

26 Dec 2019, 08:19

SetCapsLockState uses the keyboard hook to keep CapsLock off. When running multiple instances of AHK each will load the hook, with the last one started (except if run as admin) getting a higher priority processing input. So the most recently started AHK will intercept the CapsLock input, preventing it from propagating to the subsequent hooks, which will prevent hotkey conditionals from evaluating to true.

So either put everything in a single script, of look here: Add 3 more layers to your keyboard Using these 3 Modifiers - Easy to Program Too!
Florian
Posts: 30
Joined: 05 Dec 2019, 09:05

Re: Problems with SetCapsLockState, AlwaysOff when using capslock as a modifier key

26 Dec 2019, 10:11

Thank you for the thorough explanation! This clarifies so much!
One thing is not clear to me from that thread. If I use that original script at the top (as I understand this is written by you), do I have to put it into EACH script that uses capslock? Because if I only have it in 1, it doesn't work either.

Edit: Actually it doesn't work either. I'm gonna read the thread more thoroughly now.
Florian
Posts: 30
Joined: 05 Dec 2019, 09:05

Re: Problems with SetCapsLockState, AlwaysOff when using capslock as a modifier key

26 Dec 2019, 10:30

I'm just gonna put everything into 1 script. But this will be multiple thousand lines. Is there any problem with having huge scripts like that?
User avatar
Nextron
Posts: 1391
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: Problems with SetCapsLockState, AlwaysOff when using capslock as a modifier key

26 Dec 2019, 18:42

Come to think about it, the linked thread would cause trouble too when using it in multiple scripts. Perhaps you can set a passthrough hotkey for capslock up in one of the scripts which sets the proper caps lock state.
~CapsLock up::SetCapsLockState,Off
Capslock will still turn on for a moment and be visible for the other scripts, but end up in the right state.

When combining scripts; length doesn't have to a problem. The things they (simultaneously) do can. So that would be trial and error.
ufunk
Posts: 54
Joined: 17 Jan 2020, 13:45

Re: Problems with SetCapsLockState, AlwaysOff when using capslock as a modifier key

05 Mar 2020, 16:50

Hi,

I had the same problem. The suggestion of Nextron's second comment didn't work for me. I also have somewhere in my main script:

Code: Select all

SetCapsLockState, AlwaysOff
I found two solutions to that toggling problem when using capslock as hotkey is several scripts (maybe there are other solutions):

Write "SetCapsLockState, AlwaysOff" only in one script, preferably in your main script.

Then, each capslock hotkey that is not in your main script should use one of the following code structures.
I show one example for each code structure. Just for an example I use hotkeys that only apply to Excel (which can be omitted).

First example:

Code: Select all

; use the hotkey with Capslock & 1
#IfWinActive ahk_class XLMAIN	
1::   
If GetKeyState("CapsLock", "P") {
  Send, This is an example.
}
Return
Second example:

Code: Select all

; use the hotkey with Capslock & 1 (triggers when Capslock is released)
#IfWinActive ahk_class XLMAIN	
CapsLock & 1::
Keywait, Capslock
SetCapslockState, Off 
Send, This is an example.
Return
I prefer the first example, because you don't have to worry about the CapslockState.

And in general, If you want to use multiple Capslock hotkeys one below the other you can also use the following code structure:

Code: Select all

#If GetKeyState("CapsLock", "P")
a::SendInput, {LEFT}
d::SendInput, {RIGHT}
w::SendInput, {UP}
#If
ufunk
Posts: 54
Joined: 17 Jan 2020, 13:45

Re: Problems with SetCapsLockState, AlwaysOff when using capslock as a modifier key

18 May 2023, 05:37

Possibly, another way to disable that CapsLock toggles on and off (or at least to simulate that CapsLock does not toggle) is to use the following hotkey (which sets CapsLock off each time you press CapsLock):

Code: Select all

; This snippet should be at the end of the script.
*CapsLock UP:: SetCapsLockState, Off
The snippet needs to be at the end of the script, otherwise it presses somehow additional unwanted keys (I don't understood it, but the problem disappears when the snippet is at the end of the script). For example the following script would work:

Code: Select all

#If GetKeyState("CapsLock", "P")
a::SendInput, {LEFT}
d::SendInput, {RIGHT}
w::SendInput, {UP}
s::SendInput, {DOWN}
#If
Return

; This snippet should be at the end of the script.
*CapsLock UP:: SetCapsLockState, Off
I didn't try what happens when using multiple scripts with capslock as a modifier key. Maybe or maybe not you have to write *CapsLock UP:: SetCapsLockState, Off in each script, I don't know.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mesutakcan, mexican scientist, mikeyww and 132 guests