Remapping CapsLock and ^CapsLock behaves strangely Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
P1h3r1e3d13
Posts: 10
Joined: 28 Sep 2016, 02:18
Contact:

Remapping CapsLock and ^CapsLock behaves strangely

Post by P1h3r1e3d13 » 31 May 2023, 11:56

Goal: CapsLock acts as a Ctrl key (and doesn't toggle CapsLock). Except Ctrl+CapsLock, which actually toggles CapsLock. (Edit: And Ctrl still works as normal.)

Actual behavior: CapsLock does act like Ctrl, but if I hold it down for about one second, it also toggles CapsLock. Depending on the implementation, it may cycle, toggling CapsLock on and off until I release CapsLock. (Ctrl+CapsLock, does toggle CapsLock immediately, though it too may cycle if I hold it.)

I've tried all kinds of ways to do this. Some examples are below.
I've tried it with and without $ prefixes, with and without SendMode Input, with and without KeyWait
I haven't tried SendLevel & SendInput. I haven't tried AHKv2. But I'm willing to try anything.


Code: Select all

#SingleInstance Force
#NoEnv
SendMode Input

; Ctrl+CapsLock toggles CapsLock:
$^CapsLock::CapsLock

; Turn CapsLock into Ctrl:
$CapsLock::LCtrl

Code: Select all

;Auto-Execute
SetStoreCapslockMode, Off   ;makes `Send {CapsLock}` always toggle its state
return

#SingleInstance Force
#NoEnv
SendMode Input

; Ctrl+CapsLock toggles CapsLock:
$^CapsLock::
    Send {CapsLock}
Return

; Turn CapsLock into Ctrl:
$CapsLock::LCtrl

Code: Select all

#SingleInstance Force
#NoEnv
SendMode Input

; Ctrl+CapsLock toggles CapsLock:
$^CapsLock::
    Switch GetKeyState("CapsLock", "T") {
        Case 0:  ; CapsLock off
            SetCapsLockState, On
        Case 1:  ; CapsLock on
            SetCapsLockState, Off
    }
    KeyWait, CapsLock
Return

; Turn CapsLock into Ctrl:
$CapsLock::LCtrl
Last edited by P1h3r1e3d13 on 31 May 2023, 15:02, edited 1 time in total.

RussF
Posts: 1242
Joined: 05 Aug 2021, 06:36

Re: Remapping CapsLock and ^CapsLock behaves strangely

Post by RussF » 31 May 2023, 12:06

There is a much simpler way. If you are going to press the Ctrl key anyway to get capslock, why not just swap the Capslock key with the left Ctrl key? I do this on all of my devices because I believe the Ctrl key should be next to A for easy access and to prevent accidentally triggering capslock. I remap the the keys in the registry and it works for everything. It's easy to undo as well.

See this thread: viewtopic.php?f=82&t=117024 for information on how to do it.

Russ

User avatar
P1h3r1e3d13
Posts: 10
Joined: 28 Sep 2016, 02:18
Contact:

Re: Remapping CapsLock and ^CapsLock behaves strangely

Post by P1h3r1e3d13 » 31 May 2023, 15:03

Yes, and that works as expected. But I want the Ctrls to still work as Ctrl. I use Ctrl a lot, with various fingers, and CapsLock almost never. Edited the OP to clarify.

RussF
Posts: 1242
Joined: 05 Aug 2021, 06:36

Re: Remapping CapsLock and ^CapsLock behaves strangely

Post by RussF » 01 Jun 2023, 05:33

I hope you're able to achieve what you're looking for. I've read countless posts here where folks are trying to use one of the toggle keys (capslock, numlock) in ways such as you're describing and it seems it's always a struggle to make things work the way they want. By swapping the left Ctrl and the CapsLock keys outright, you're still able to use both Ctrl keys (the right one isn't affected at all) and in fact, it's easier to add Ctrl to any of the keys on the left half of the keyboard because you can reach all the keys on the left side of the keyboard with your left pinkie on the (old) CapsLock (now) Ctrl, without moving your left hand away from the home row or twisting uncomfortably. Earlier keyboards had the Ctrl key next to A for that very reason, because it is used far more than CapsLock for most people.

Perhaps someone else here can help you reach your goal. Cheers,

Russ

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

Re: Remapping CapsLock and ^CapsLock behaves strangely  Topic is solved

Post by mikeyww » 01 Jun 2023, 06:00

Code: Select all

#Requires AutoHotkey v1.1.33
#If !GetKeyState("Ctrl", "P")
CapsLock::LCtrl
#If

User avatar
P1h3r1e3d13
Posts: 10
Joined: 28 Sep 2016, 02:18
Contact:

Re: Remapping CapsLock and ^CapsLock behaves strangely

Post by P1h3r1e3d13 » 02 Jun 2023, 11:32

And now my last example is working. I'm not sure what was wrong. (I suspect something about the auto-execute section and how the files are included by AutoInclude.ahk.)

Here is my final, working version:

Code: Select all

#SingleInstance Force ;#SingleInstance is implicit in hotkey scripts, but Force switch is not
#NoEnv
SendMode Input

; Turn CapsLock into Ctrl:
$CapsLock::LCtrl

; Turn left Ctrl into CapsLock:
;$LCtrl::CapsLock

; Toggle CapsLock with Ctrl+CapsLock:
$^CapsLock::
    Switch GetKeyState("CapsLock", "T") {
        Case 0:  ; CapsLock off
            SetCapsLockState, On
            ;SoundBeep, 1980, 150
        Case 1:  ; CapsLock on
            SetCapsLockState, Off
            ;SoundBeep, 1020, 150
    }
    KeyWait, CapsLock
Return

User avatar
P1h3r1e3d13
Posts: 10
Joined: 28 Sep 2016, 02:18
Contact:

Re: Remapping CapsLock and ^CapsLock behaves strangely

Post by P1h3r1e3d13 » 02 Jun 2023, 11:39

mikeyww wrote:
01 Jun 2023, 06:00

Code: Select all

#Requires AutoHotkey v1.1.33
#If !GetKeyState("Ctrl", "P")
CapsLock::LCtrl
#If
Oh shoot, that's way simpler! And it solves the repeat problem: I can hold Ctrl and press CapsLock repeatedly and it toggles each time. With my version, I had to release Ctrl and start over. Thank you!

Post Reply

Return to “Ask for Help (v1)”