Using Caps Lock as Menu/Apps key on Emacs

September 3, 2015, by Felipe Lessa (meteficha)

I’m an ergoemacs-mode user, a mode that changes most key bindings so that they put less strain on your hands. For example, it uses Alt instead of Ctrl most of the time, which is easier to press: use your curled thumb instead of a karate chop. Also, many commands are activated by first pressing the Menu/Apps key (that key near the Right Ctrl which usually opens the context menu). For example, pressing Menu then T allows you to switch buffers.

However, the keyboard on my new notebook doesn’t have a dedicated Menu key. Instead, one needs to press Fn+Right Ctrl, which is of course extremely painful.

Picture of menu key on my notebook Menu key hidden on the Right Ctrl.

I’ve found a workaround, though. A very hackish workaround.

The ergoemacs-mode FAQ suggests using Caps Lock as a Menu/Apps key for Mac users. Using xmodmap it’s trivial to make Caps Lock a Menu key:
$ xmodmap -e "keycode 66 = Menu"

However, using xmodmap properly with Gnome is nigh impossible. It’s recommend to use xkb instead, but xkb doesn’t support mapping Caps Lock to the Menu key out-of-the-box (at least not yet). At this point, having wandered through many documentation pages, I’ve decided to try using some of the xkb options that already exist.

At first I tried setting Caps Lock as the Hyper key. However, by default the Hyper key gets the same modifier code as the Super key (which is usually the key with the Windows logo). There’s a straightforward way of separating them, but I couldn’t find a way to make it play nice with Gnome. And even if I could, it’s not clear to me if I could use the Hyper key as a substitute for the Menu key on emacs.

When ready to admit defeat, I’ve set the Caps Lock behavior to “Caps Lock is disabled” in preparation of trying a hack using xmodmap. Much to my surprise, I accidentally discovered that emacs then began treating the disabled Caps Lock key as <VoidSymbol>! The gears started turning in my head, then I added the following line to my ~/.emacs file:
(define-key key-translation-map (kbd "<VoidSymbol>") (kbd "<menu>"))

Surprisingly, it worked! Now pressing Caps Lock then T will switch buffers, for example. As a bonus, pressing Caps Lock accidentally while on another application won’t do anything.

It’s not clear to me how fragile this hack really is. I’ll update this blog post if I ever find some drawback to it. But right now it seems to work quite nicely.

2016-01-02: Still working fine, didn’t have any issues with this hack so far :).