💎 Productivity for coders: Caps Lock as a keyboard layout switcher
Tags: beginners, productivity, tutorial, dx
Published: 2021-03-01 05:39:28

## 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! 😅

<a name="toc"></a>
### 📝 Table of contents

- [What are we trying to solve?](#what-are-we-trying-to-solve)
- [How do we solve this?](#how-do-we-solve-this)
  - [The Apple macOS solution](#the-apple-macos-solution)
  - [The GNU/Linux solution](#the-gnu_linux-solution)
  - [The MS Windows solution](#the-ms-windows-solution)

<a name="#what-are-we-trying-to-solve"></a>
## 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](#toc)

<a name="#how-do-we-solve-this"></a>
## How do we solve this?

For me, a great solution was to change the keyboard layout from the two-button <kbd>CMD</kbd> + <kbd>SPACE</kbd> (_for macOS_) and <kbd>ALT</kbd> + <kbd>SPACE</kbd> (_for GNU/Linux_) or <kbd>ALT</kbd> + <kbd>SHIFT</kbd> (_for MS Windows_) to <kbd>CAPS LOCK</kbd>. 

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](#toc)

<a name="#the-apple-macos-solution"></a>
### The Apple macOS solution

> Original solution from StackExchange is [here](https://apple.stackexchange.com/a/303773).

Create a new [Bash](https://en.wikipedia.org/wiki/Bash_(Unix_shell)) 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_".

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

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

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 <kbd>F18</kbd>) for <kbd>CAPS LOCK</kbd> button, which will be valid for the current user session.

Let's make it executable:

```bash
chmod +x ~/.config/keyboard-layout/capslock_f18.sh
```

![macOS Caps Lock as keyboard layout switcher](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u7039uh2zkma63y1n0zc.png)

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

```xml
<!-- ~/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>
```

> ☝️ Please note, I use [zsh](https://en.wikipedia.org/wiki/Z_shell) Unix shell. If you use a different one, specify it.

Save it and run a new service:

```bash
launchctl load ~/Library/LaunchAgents/com.user.loginscript.plist
```

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

[↑ Table of contents](#toc)

<a name="#the-gnu_linux-solution"></a>
### The GNU/Linux solution

Create a new [X.Org Server](https://en.wikipedia.org/wiki/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:

```bash
# /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
```

> ☝️ 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](#toc)

<a name="#the-ms-windows-solution"></a>
### 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](https://www.autohotkey.com/) (Auto Hot Key) program. In this way, pressing <kbd>CAPS LOCK</kbd> button will emulate pressing <kbd>ALT</kbd> + <kbd>SHIFT</kbd>.

Create a new AHK script:

```bash
CapsLock::Send, {Alt Down}{Shift Down}{Shift Up}{Alt Up}
```

Wow, that's it! 🎉

[↑ Table of contents](#toc)

<a name=""></a>
## Photos by

- Vic Shóstak (author) https://github.com/koddr

<a name=""></a>
## P.S.

If you want more articles (like this) on this blog, then post a comment below and subscribe to me. Thanks! 😻

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 projects that need your help (and stars) 👇

- 🔥 [gowebly][gowebly_url]: A next-generation CLI tool for easily build amazing web applications with Go on the backend, using htmx & hyperscript and the most popular atomic/utility-first CSS frameworks on the frontend.
- ✨ [create-go-app][cgapp_url]: Create a new production-ready project with Go backend, frontend and deploy automation by running one CLI command.
- 🏃 [yatr][yatr_url]: Yet Another Task Runner allows you to organize and automate your routine operations that you normally do in Makefile (or else) for each project.
- 📚 [gosl][gosl_url]: The Go Snippet Library provides snippets collection for working with routine operations in your Go programs with a super user-friendly API and the most efficient performance.
- 🏄‍♂️ [csv2api][csv2api_url]: The parser reads the CSV file with the raw data, filters the records, identifies fields to be changed, and sends a request to update the data to the specified endpoint of your REST API.
- 🚴 [json2csv][json2csv_url]: The parser can read given folder with JSON files, filtering and qualifying input data with intent & stop words dictionaries and save results to CSV files by given chunk size.

<!-- Footer links -->

[gowebly_url]: https://github.com/gowebly
[cgapp_url]: https://github.com/create-go-app
[yatr_url]: https://github.com/koddr/yatr
[gosl_url]: https://github.com/koddr/gosl
[csv2api_url]: https://github.com/koddr/csv2api
[json2csv_url]: https://github.com/koddr/json2csv