Long press CapsLock act as Ctrl

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
sudhirkhanger
Posts: 2
Joined: 22 Jul 2022, 22:06
Contact:

Long press CapsLock act as Ctrl

Post by sudhirkhanger » 15 Sep 2022, 20:47

Hello,

I am a bit of a noob with AHK. On Mac, I used Karabiner to make CapsLock (and Enter/Return) as follows.

* Single press works as CapsLock
* Ctrl works as usual. Its behavior is not changed.
* When CapsLock is long pressed along with another key, then it acts as Ctrl. (If long pressed and released without any other key pressed then it continues to act as CapsLock - this part is not mandatory per se)

Any help will be appreciated.

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

Re: Long press CapsLock act as Ctrl

Post by mikeyww » 15 Sep 2022, 21:31

Welcome to this AutoHotkey forum!

Perhaps:

Code: Select all

*CapsLock::
SetKeyDelay -1
Send {Blind}{Ctrl DownR}
Return

*CapsLock Up::
SetKeyDelay -1
Send {Blind}{Ctrl up}
If (A_PriorKey = "CapsLock")
 SetCapsLockState, % !GetKeyState("CapsLock", "T")
Return

kuehmaster
Posts: 3
Joined: 31 Aug 2022, 22:38

Re: Long press CapsLock act as Ctrl

Post by kuehmaster » 02 Oct 2022, 01:54

Hi,

Then how would you make it such that by holding down the capslock key it sends control, but by tapping the key still acts like a capslock?

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

Re: Long press CapsLock act as Ctrl

Post by mikeyww » 02 Oct 2022, 07:10

I would make it like I did. Here is what the script does.

* Single press works as CapsLock
* Ctrl works as usual. Its behavior is not changed.
* When CapsLock is long pressed along with another key, then it acts as Ctrl. (If long pressed and released without any other key pressed then it continues to act as CapsLock - this part is not mandatory per se)

GEV
Posts: 1002
Joined: 25 Feb 2014, 00:50

Re: Long press CapsLock act as Ctrl

Post by GEV » 02 Oct 2022, 08:52

@mikeyww,
could you please explain this
SetKeyDelay says
When the delay is set to -1, a script's process-priority becomes an important factor in how fast it can send keystrokes when using the traditional SendEvent mode.
It's not clear to me.
And what does SetKeyDelay -1 in your answer to this question? I mean what is the importance of it in this context?

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

Re: Long press CapsLock act as Ctrl

Post by mikeyww » 02 Oct 2022, 09:33

The script is adapted from the documentation. The -1 eliminates the usual delay after the key is sent. You can probably delete the line without any adverse effect in this setting.

sudhirkhanger
Posts: 2
Joined: 22 Jul 2022, 22:06
Contact:

Re: Long press CapsLock act as Ctrl

Post by sudhirkhanger » 22 Oct 2022, 23:07

Thank you, @mikeyww. This has been working well. I would say 98% times. Sometimes it starts acting wonky. I am not able to replicate when it stops working, but I can exit and restart the shortcut, and it starts working again.

I have just disabled Sticky, Toggle, and Filter key and their shortcut. Let's see if it makes any difference.

Can this also be replicated for Enter/Return key with similar behavior?

* Single press enter key acts like enter.
* Simultaneous enter and another-key press acts like Ctrl.

This will help me use either of my pinky fingers for pressing Ctrl combination which is used a lot for text manipulation in Emacs.

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

Re: Long press CapsLock act as Ctrl

Post by mikeyww » 23 Oct 2022, 06:11

Code: Select all

*Enter::Send {Blind}{Ctrl DownR}        ; Use ENTER as Ctrl key
*Enter Up::
RegExMatch(A_ThisHotkey, "\*\K\S+", hk) ; Enter
Send % "{Blind}{Ctrl up}" (A_PriorKey = hk ? "{" hk "}" : "")
Return

Post Reply

Return to “Ask for Help (v1)”