DEV Community

loading...
Cover image for 馃拵 Productivity for coders: Caps Lock as a keyboard layout switcher

馃拵 Productivity for coders: Caps Lock as a keyboard layout switcher

Vic Sh贸stak
Hey! 馃憢 I'm a Software Engineer and founder of a friendly outsource company called True web artisans, with excellent UX/UI knowledge and over 12 years of practical experience.
Updated on 銉4 min read

Introduction

Hey, everyone! 馃檪 It's time to start (yet another) series of articles describing various techniques and tricks, that increase coders productivity.

The first article of the series will be devoted to the keyboard layout and will be relevant to those programmers for whom English is not their native language.

For example, like me! 馃槄

馃摑 Table of contents

What are we trying to solve?

Think for a moment, how many times a day do you have to switch from one language to another? A lot, right! Okay, now remember, how often you have to type something in capital letters for a long time?

Probably a few times and that's because you need to write a SQL query or an angry comment in the task tracker (just kidding, be positive 馃槈)!

馃挰 In other words, your keyboard has a very handy ring or pinky finger button that you rarely use and could serve much better.

鈫 Table of contents

How do we solve this?

For me, a great solution was to change the keyboard layout from the two-button CMD + SPACE (for macOS) and ALT + SPACE (for GNU/Linux) or ALT + SHIFT (for MS Windows) to CAPS LOCK.

But it was not possible to do it by standard means through the settings (UI). That's why a little hack via console, config files and special software.

鈫 Table of contents

The Apple macOS solution

Original solution from StackExchange is here.

Create a new Bash script file with name capslock_f18.sh and place him to exist hidden folder ~/.config/keyboard-layout on your macOS system:

# ~/.config/keyboard-layout/capslock_f18.sh

hidutil property --set '{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x700000039,"HIDKeyboardModifierMappingDst":0x70000006D}]}' > /dev/null 2>&1
Enter fullscreen mode Exit fullscreen mode

鈽濓笍 You can place the file anywhere else! The main thing is not to forget to specify the path to it in the settings of the .plist file.

This script programmatically sets a different key code (in this example F18) for CAPS LOCK button, which will be valid for the current user session.

Let's make it executable:

chmod +x ~/.config/keyboard-layout/capslock_f18.sh
Enter fullscreen mode Exit fullscreen mode

macOS Caps Lock as keyboard layout switcher

Next, create file ~/Library/LaunchAgents/com.user.loginscript.plist with the following content (you must have administrator rights):

<!-- ~/Library/LaunchAgents/com.user.loginscript.plist -->

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
   <key>Label</key>
   <string>com.user.loginscript</string>
   <key>ProgramArguments</key>
   <array>
        <string>zsh</string>
        <string>-c</string>
        <string>~/.config/keyboard-layout/capslock_f18.sh</string>
   </array>
   <key>RunAtLoad</key>
   <true/>
   <key>ThrottleInterval</key>
   <integer>86400</integer>
</dict>
</plist>
Enter fullscreen mode Exit fullscreen mode

鈽濓笍 Please note, I use zsh Unix shell. If you use a different one, specify it.

Save it and run a new service:

launchctl load ~/Library/LaunchAgents/com.user.loginscript.plist
Enter fullscreen mode Exit fullscreen mode

Okay. Finally, reboot you Mac or logout. That's it! 馃帀

鈫 Table of contents

The GNU/Linux solution

Create a new X.Org Server config file with name 00-keyboard.conf and place him to a folder /usr/share/X11/xorg.conf.d on your Linux system:

# /usr/share/X11/xorg.conf.d/00-keyboard.conf

Section "InputClass"
    Identifier "system-keyboard"
    MatchIsKeyboard "on"
    Option "XkbLayout" "us,ru"
    Option "XkbModel" "pc105"
    Option "XkbOptions" "grp:caps_toggle,grp_led:caps"
EndSection
Enter fullscreen mode Exit fullscreen mode

鈽濓笍 Please note, you can set option XkbLayout to your languages range, as you wish, like en,fr or else.

Yep, just reboot you PC. That's it! 馃帀

鈫 Table of contents

The MS Windows solution

Of all the ways I've tried to do this in a Microsoft operating system, I liked the way through the AHK (Auto Hot Key) program. In this way, pressing CAPS LOCK button will emulate pressing ALT + SHIFT.

Create a new AHK script:

CapsLock::Send, {Alt Down}{Shift Down}{Shift Up}{Alt Up}
Enter fullscreen mode Exit fullscreen mode

Wow, that's it! 馃帀

鈫 Table of contents

Photos by

P.S.

If you want more 鈫 write a comment below & follow me. Thanks! 馃槝

Discussion (2)

Collapse
gamecube762 profile image
Gamecube762

For Windows with AHK, I suggest adding SetCapsLockState, AlwaysOff to prevent CapsLock getting stuck in the On state.

I also like making use of SetNumLockState, AlwaysOn and SetScrollLockState, AlwaysOff so I can rebind those keys without worrying about getting stuck in unwanted states.

Collapse
koddr profile image
Vic Sh贸stak Author

Thanks for share this!