0

An AutoHotkey question.

In general, I would like to see, when CapsLock is on, pressing one key would produce Shift + key. My question is almost answered in a SuperUser question here and an AutoHotkey post here.

With the help there, what I can achieve now is

  1. when CapsLock is on, pressing a would produce A, and likewise for other letters;
  2. when CapsLock is on, pressing 1 would produce !, 2 would produce @, and likewise for other keys with shifted symbols, e.g. - =;

The special part of my question is that I want to see this behavior happen with arrow keys.

To be specific:

  1. when CapsLock is on, pressing Right arrow key would produce Shift + Right, and likewise for Left, Up, and Down;
  2. when CapsLock is on, pressing Ctrl + Right would produce Ctrl + Shift + Right;
  3. In fact, I have remapped keys with LAlt & L::Send {Right} so that Alt + L would produce the Right arrow key. Now I want to see, when CapsLock is on, Alt + L would produce Shift + Right.

For your information, I put a solution by Icarus from AutoHotkey forum here

#SingleInstance Force

Keys := "abcdefghijklmnopqrstuvwxyz1234567890-=[];'\,./"
Loop parse, keys
    Hotkey $%A_LoopField%, PressKey

Return

PressKey:
    StringReplace ThisHotkey, A_ThisHotkey, $
    If( GetKeyState( "CAPSLOCK", "T" ) )
        SendInput +%ThisHotkey%
    Else
        SendInput %ThisHotkey%  

Return

ESC::ExitApp

It works for point 1 and point 2, but I do not know how to adapt it for arrow keys for points 3, 4 and 5. I tried appending {Right} to Keys in the above script but it won't work.

Any help is highly appreciated!

1

Somehow managed to solve it myself. Excited!

The key is to not put Right in curly braces (like {Right}) when it stands on the left-hand side of :: as the originKey.

Solutions corresponding to points 3 to 5 in my own question:

; Solution to Point 3
Left::SendInput % GetKeyState("CAPSLOCK", "T")  ? "+{Left}"  : "{Left}"
Up::SendInput % GetKeyState("CAPSLOCK", "T")    ? "+{Up}"    : "{Up}"
Down::SendInput % GetKeyState("CAPSLOCK", "T")  ? "+{Down}"  : "{Down}"
Right::SendInput % GetKeyState("CAPSLOCK", "T") ? "+{Right}" : "{Right}"

; Solution to Point 4
LCtrl & Left::SendInput % GetKeyState("CAPSLOCK", "T")  ? "^+{Left}"  : "^{Left}"
LCtrl & Up::SendInput % GetKeyState("CAPSLOCK", "T")    ? "^+{Up}"    : "^{Up}"
LCtrl & Down::SendInput % GetKeyState("CAPSLOCK", "T")  ? "^+{Down}"  : "^{Down}"
LCtrl & Right::SendInput % GetKeyState("CAPSLOCK", "T") ? "^+{Right}" : "^{Right}"

; Solution to Point 5
LAlt & J::SendInput % GetKeyState("CAPSLOCK", "T") ? "+{Left}"  : "{Left}"
LAlt & I::SendInput % GetKeyState("CAPSLOCK", "T") ? "+{Up}"    : "{Up}"
LAlt & K::SendInput % GetKeyState("CAPSLOCK", "T") ? "+{Down}"  : "{Down}"
LAlt & L::SendInput % GetKeyState("CAPSLOCK", "T") ? "+{Right}" : "{Right}"

There might be concise ways to write the above script, which is most welcome :)

1
#NoEnv
#SingleInstance Force

/* 
; If your keyboard has no indicator for Caps Lock, you can use this GUI:
Gui, Capslock: -caption +AlwaysOnTop
Gui, Capslock: Add, Text, x5 y5, Capslock is ON
If GetKeyState("Capslock","T")
    Gui, Capslock: Show, x0 y0
*/

Keys := ["Right","Left","Up","Down","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","1","2","3","4","5","6","7","8","9","0","-","=","[","]",",","\",",",".","/",";"]
for each, key in Keys
    Hotkey, $%key%, Shift_Key
return

Shift_Key:
ThisHotkey := StrReplace(A_ThisHotkey, "$")
If GetKeyState("Capslock","T")
    SendInput, +{%ThisHotkey%}
else
    SendInput, {%ThisHotkey%}
return

/* 
~Capslock Up::
If GetKeyState("Capslock","T")
    Gui, Capslock: Show, x0 y0
else
    Gui, Capslock: cancel
return
*/
  • Thanks! I know very little about AHK's syntax. Do we have to write it like "a","b","c","d","e","f","g", or can we write it like "abcdefg" and then somehow add "Right" to the list? – fleix Jan 3 at 12:59
  • I'd reccomend to use arrays (objects) instead of alternative methods. Arrays give you many features that other methods do not have. – user3419297 Jan 3 at 13:50

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.