Sunday, 7 June 2009

Death to Caps Lock



Even though it is cruise control for cool, personally I think it is the number one most useless key on modern keyboards. "Why not Scroll Lock or Pause/Break?" you may ask; well, you see, the Caps Lock key is in a fantastic position - usually directly next to the home row, and as large if not larger than a Control key. That position could be used for much more useful keys; for example in the case of programming or general UNIX-usage, Control. On OS X the Command (Apple) key is perhaps a better candidate, due to the preference of Command over Control for general OS and application shortcuts. Scroll Lock and Pause/Break are simply too small and too far away to be annoying, as useless as they may be.

If you've yet to try remap your Caps Lock key, I would highly recommend it. Once you get used to having the convenience of a Control or Command key essentially at the natural resting place of your left pinky you really cannot live without it. Productivity is greatly improved when you're able to copy/paste or save files or close applications or terminate commands or logout or an infinity of other commands depending on what apps you use in the blink of an eye, without scrunching your hand and probably giving yourself huge cause for RSI in the future.

The problem is, how do you actually go about remapping your Caps Lock key? Google gives thousands of results on the subject but they're usually fragmented by operating system and have a tendency to get outdated with alarming regularity. So here I hope to document the procedure for several major operating systems in a single place, for as much my own reference as anyone else's. Without further ado:

OS X

Very simple; open System Preferences and go to Keyboard & Mouse. On the Keyboard tab, click the Modifier Keys... button. This pops up a simple dialog allowing you to remap all your modifier keys; in the case of Caps Lock this is easily changed to Command or Control, depending on your preference. Changes should take effect immediately.

Windows

Short of manually hacking scan codes in the registry you're going to need a third-party application to do the job. One such app that I've been using for years now is the excellent SharpKeys by Randy. Another such tool is KeyTweak, although I've yet to use it so can't comment. All these tools are essentially pretty frontends to the magical HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout registry key that controls how raw key codes are mapped to actually key presses in Windows.

In SharpKeys you can simply add a mapping from Special: Caps Lock to Special: Left Ctrl (you can press Type Key when adding a mapping to simply press the keys you want to map without hunting for them in the huge list of keys), and click Write to Registry to save the changes. You'll need to logout and back in for the changes to be registered.

UNIX derivatives (Linux/BSD)

The method for accomplishing the mapping in virtual terminals (i.e. non-X modes) is specific to individual distributions so I'll only cover the ones I currently use here, which are Ubuntu and FreeBSD. For X use, with a modern Xorg installation that uses HAL the process is the same no matter what the distribution -- in this case both of Ubuntu 9.04 (Jaunty Jackalope) and FreeBSD 7.x. Your distro/window manager of choice might include a pretty GUI frontend to accomplish a similar task, which I won't cover here. The methods I use are to ensure that no matter how I use a machine the keys are consistently mapped; both in X (regardless of window manager) and on regular virtual terminals, regardless of which user I'm logged in as at the time.

X.Org X11

As of X.Org release 7.3, the HAL system is used to provide hotplug support of input devices. The (at first) frustrating consequence is all your preexisting InputDevice sections in xorg.conf are not used, so the old way of adding an Option "XkbOptions" "ctrl:nocaps" line to an InputDevice keyboard section will not work (well, there is a way to get it to work in the old style, but this method may not be around for much longer so it's not advisable to use it).

The new way is to use a HAL policy to match any keyboards that get plugged in and perform the remapping there. The old X11-style options are carried over almost verbatim so the names and values of such a policy should be familiar, even though it's now an XML configuration file.

Create a file with extension .fdi in /etc/hal/fdi/policy (/usr/local/etc/hal/fdi/policy on FreeBSD) with the following contents:


<?xml version="1.0" encoding="UTF-8"?>
<deviceinfo version="0.2">
<device>
<match key="info.capabilities" contains="input.keyboard">
<merge key="input.xkb.Options" type="string">ctrl:nocaps</merge>
</match>
</device>
</deviceinfo>


If the above doesn't work after a HAL restart you can also try input.x11_options.XkbOptions instead of input.xkb.Options, which allows you to directly pass old-style X11 options through.

On FreeBSD you'll also need to ensure HAL and DBUS are running in order for this method to work.

Here's a post on the Ubuntu forums with further discussion on the issue.

Ubuntu Virtual Terminals
As described in this bug the process is pretty simple. All you need to do is add the following line to /etc/default/console-setup:

XKBOPTIONS="ctrl:nocaps"

then do a

# dpkg-reconfigure console-setup

to persist the change.

You're also able to directly hack /etc/console-setup/boottime.kmap.gz to perform whatever additional mapping you may require, although for mapping caps lock to control the above method is far easier & safer.

FreeBSD Virtual Terminals

Several keymappings are available by default in /usr/share/syscons/keymaps; in the case of the US and UK layouts versions already exist that have caps lock mapped as the left control key. To use for example the UK layout it's as simple as adding the following line to /etc/rc.conf

keymap='uk.iso-ctrl.kbd'

and rebooting for the change to take effect.

If no such preconfigured layout exists for your language/setup then it's easy enough to copy one of the preexisting layouts and simply change the Clock mapping to that of Control (while maintaining character codes!), then change the above keymap setting to point to your new layout.

1 comments:

Patrick_J said...

Windows Method: Use a knife. Jam the knife under the key and lift. Use force.

In all seriousness, i have actually removed a key off the keyboard on purpose -- i accidently used to hit it.

Another useless key (on a Lenovo) are the damn page scroll buttons above the arrows keys...which cause VS to jump an untold number of pages, and then scroll down.