DEV Community

Cover image for 💎 Productivity for coders: Caps Lock as a keyboard layout switcher
Vic Shóstak
Vic Shóstak

Posted on • Updated on

💎 Productivity for coders: Caps Lock as a keyboard layout switcher

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:

☝️ Please note: This solution works only on macOS 10.14 (Mojave) and lower. For modern Mac systems you can go to the keyboard's settings tab called "Input Sources", and click checkbox "Use the Caps Lock key to switch to and from ABC".

# ~/.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 articles (like this) on this blog, then post a comment below and subscribe to me. Thanks! 😻

❗️ You can support me on Boosty, both on a permanent and on a one-time basis. All proceeds from this way will go to support my OSS projects and will energize me to create new products and articles for the community.

support me on Boosty

And of course, you can help me make developers' lives even better! Just connect to one of my projects as a contributor. It's easy!

My main projects that need your help (and stars) 👇

  • 🔥 gowebly: A next-generation CLI tool that makes it easy to create amazing web applications with Go on the backend, using htmx, hyperscript or Alpine.js and the most popular CSS frameworks on the frontend.
  • create-go-app: Create a new production-ready project with Go backend, frontend and deploy automation by running one CLI command.

Other my small projects: yatr, gosl, json2csv, csv2api.

Top comments (3)

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

Thanks for share this!

Collapse
 
qodunpob profile image
Konstantin

I use PowerToys for Windows. And this is an incredible tool that also lets you remap keys.