On Fixing Apple's Hardware: The Apple Keyboard Edition, Part 1

I recently picked up an Apple Keyboard (model A1242 — sans numeric keypad) for the office. I was thinking of getting another Happy Hacking Keyboard Lite 2, but it turns out I seem to use function keys these days and I do not want to have to use a hotkey to get to them.

One problem with Apple Keyboard is that they have the Alt and Super keys swapped, which for anyone using Emacs-style key bindings is a pain in the arse. Ubuntu's wiki goes into some detail about how to fix this and the AKBD's other egregious problems, but the part about fixing Alt and Super were written by space cadets — they are only partially correct and seem to come with a bag of the author's pet binding changes, too. So I have had to roll my own.

The easiest way to fix this is to use xmodmap to redefine the keycode mapping. This is straight forward once you know what the key codes are, they can be discovered by running xev and hitting the keys in question. The minimal xmodmap configuration I am using is below:

! Swap Alt and Cmd keys.
keycode 133 = Alt_L Meta_L
keycode 134 = Multi_key
keycode 64  = Super_L
keycode 108 = Super_R

clear Mod1
add   Mod1 = Alt_L

clear Mod4
add   Mod4 = Super_L Super_R

This swaps the Super and Alt keys, as well as preserving the new Right Alt as the compose key, as nature intended it. Save this in a file and point xmodmap at it. If you name the file ~/.Xmodmap, it might even be automatically read when you start your X session, but hey: All care taken, no guarantee given.

Of course, this works great until I unplug the AKBD and return to using a normal keyboard, so I need to be able to undo the fix. For this, I use the complement of the above:

! Swap Alt and Cmd keys.
keycode 64  =  Alt_L Meta_L
keycode 108 =  Multi_key
keycode 133 =  Super_L
keycode 134 =  Super_R

clear Mod1
add   Mod1 = Alt_L

clear Mod4
add   Mod4 = Super_L Super_R

I put two launchers on my panel that runs xmodmap on each of the configurations above. The next step — Part 2 of this enthralling series, will be to have udev automatically execute these when the keyboard is plugged in or removed. Stay tuned.