Wednesday, July 4, 2007

Remapping CTRL and CAPS LOCK in Windows Vista

As any Unix geek will quickly tell you, one of the greatest frustrations of serious geeks with the PC keyboard is the placement of the CTRL key and the CAPS LOCK key. Look at your PC keyboard, you'll see the CAPS LOCK by your "a" key and the CTRL key waaaay down in the bottom left (and usually also on the right). That wasn't the way it was back in the day. Once upon a time, the CAPS LOCK and CTRL keys were switched.

Now, I'm too young to have cut my teeth on Sparc stations, mainframes, mid-ranges, or minis back in the days of BSD and AT&T Unix, where these keyboard configurations were standard. However, I've read about them in various Linux & FreeBSD mailing lists, websites, etc. Amongst the Linux crowd, it's still a big todo.

Having carpal tunnel syndrome and being an avid Linux CLI user, the concept was always appealing to me. For those that don't know, the CTRL key is used heavily in advanced CLI operations (e.g., CTLR-L will clear your terminal screen, CTRL-U will clear the line of text you're typing, CTRL-K will clear the text after your cursor, etc.)

Since it's so heavily used, putting the CTRL key next to the "a" key makes a lot of sense. One doesn't have to move too far from the home keys in order to hold it down while pressing the other key of interest. One also strikes the CTRL key far more frequently than the CAPS LOCK key. Less movement of the fingers is a BIG and GOOD thing when having to deal with RSI. That's the same reason I'm teaching myself the Dvorak keyboard...but that's a story for another post... ;-)

So, switching the CTRL key with the CAPS LOCK key is a very good idea, for those of us who live on the command line interface.

In either case, the remapping is easy enough to achieve in any Linux distro. I had thought I could achieve the same in Windows using Mark Russinovich's CTRL2CAP. It's worked well for me in the past, on Win2K and even WinXP. However, my new Toshiba is running the dreaded V, and I face the imminent and depressing prospect of having to use the same at work. So, finding a way to make the switch was somewhat important to me.

After a thorough job of Googling, I found this link with instructions to directly manipulate the Windows registry to achieve the switch in WinNT and Win2K. I was wary about it working, since when I clicked through on the in-depth technical background on the registry tweak referenced a mailing list posting from 1996 that talked about the hack definitively working on NT 4.0, and maybe on 3.5...!

Still, I figured, what's the worst that could happen?

The answer is, nothing bad happens. I got it wrong the first few times and nothing failed or crashed, it simply didn't change anything. Once I got it right, it worked like a champ on Windows Vista. I fixed my keyboard problem, my CTRL key is now completely switched with my CAPS LOCK. I'm now a much happier and healthier geek.

Oh, just in case the page evaporates, as things are wont to do on the Internet from time to time, here's the original posting in full:
Windows Registry Key-remap
Changing NT and Win2K Keymaps through the Registry

1. Run Start->Run...
2. Open: regedt32 to edit the registry.
3. Select the key

HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Keyboard Layout


4. From the Edit menu, choose "Add Value" to add a value named "Scancode Map" with type REG_BINARY.
5. Set the value for the layout you want (copy and paste the bold text below):
* Map CapsLock -> Left Ctrl:
0000000000000000020000001d003a0000000000
* Swap CapsLock and Left Ctrl:
0000000000000000030000001d003a003a001d0000000000
* CapsLock->Left Ctrl, Left Ctrl->Left Alt, Left Alt->CapsLock:
0000000000000000040000001d003a0038001d003a00380000000000
6. Reboot your machine


I tried the third option, to do a full three-key switch between CTRL, ALT, and CAPS LOCK, but immediately realized it was a mistake. The ALT key is used too frequently for things like ALT-TAB window switching. It's native placement by the space bar makes it handy to hit with the thumb, which is the strongest digit on the hand - a good thing for those of us with carpal tunnel.

In addition, here's the original email that the original poster references. It's fairly interesting.
Scancode Map Email

From: Shane Holder <holder@mordor.rsn.hp.com>
To: "Jeff McCarrell" <jwm@cccpp.com>
Cc: cmcmahan@Teknowledge.COM, Jonathan Payne <jpayne@marimba.com>, ntemacs-users@cs.washington.edu
Subject: Re: Re[2]: problem with caps/ctrl swap on NT 4.0
Date: 04 Dec 1996 14:36:21 -0600


Found this on the net somewhere.

Add this value:

HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\KeyBoard
Layout\Scancode Map

It's a binary value that lets you map keystrokes in the low-level keyboard drivers in NT. As a result you don't have to worry about applications bypassing mappings that you've done at a higher level (i.e. it just works).

Here's the format of the value:

DWORD: 0x00000000 header
DWORD: 0x00000000 header
DWORD: length (in DWORDs) of remaining data, including terminating DWORD
DWORD: mapping 1
...
DWORD: mapping n
DWORD: 0x00000000 terminating null DWORD

Each mapping DWORD has two parts: the input scancode, and an output scancode. To map scancode 0x1d (left control) to scancode 0x3a (caps lock), you want a value of 0x003a001d. Note that this does not swap the keys. Using just this mapping value, both the left control and the caps lock key will behave as caps-lock. To swap, you also need to map 0x3a to 0x1d, using 0x001d003a. So, the complete registry value you'd use to swap left-control and caps-lock is:

00 00 00 00 00 00 00 00 03 00 00 00 1d 00 3a 00 3a 00 1d 00 00 00 00 00

This works on NT 4.0, I don't know about 3.51. This registry value is system wide, and can't be made user-specific. It also only takes affect on reboot.




--
Shane Holder e-mail: holder@rsn.hp.com
Hewlett Packard phone: (214)497-4182
3000 Waterview Never underestimate the bandwidth
Richardson, TX 75083 of a truck moving at 70 MPH.


So, I hope this posting helps some other poor geek who's tearing out his hair about how to make Windows Vista somewhat tolerable to work within. (I'd say work with, but it's more similar to operating within the confines of a cage, really...)