After having lived with some annoying CAPS-LOCK mishaps and a general feeling of pain in the lower forearm, I decided to remap my keyboard layout for a smoother programming experience. Since I'm Norwegian and use a Norwegian keyboard layout, I have to resort to AltGr+0 to produce a }. Brackets are equally far away. Parenthesis are shift+8 and shift+9 on the number row and all this means moving the hand away from the home row a lot. I wanted to be able to program and maintain my hands in a more or less touch typing position.The goal was improved speed at programming and less discomfort. The fact that I daily program on both windows and linux ( both native and via the cygwin X server) made things a bit more complex than anticipated, as we will see. This article tackles re-mapping the caps-lock key in windows and creating a new keyboard layout in both windows and linux (Xorg that is). Info about re-mapping the caps-lock key under linux is just a google search away. The following setup was used in this little adventure:
  • Windows XP running Cygwin.
  • Ubuntu 9.04 (as X client only)
  • Xorg server 1.5.7 with Xkb packages running in cygwin.

Caps lock remapping

The caps lock key is irritating and I've never used as far as I can tell. But I haven't gotten around to doing anything about it. Now's the time. First I thought about remapping it to control and binding a lot the characters I want more accessible to ctrl+ bindings. This messes with application shortcuts a lot so I went for altGr instead. I used a small program named KeyTweak to perform the binding. The result of running that program is that i new value is written to the registry. It looks like this:
Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout]

"Scancode Map"=hex:00,00,00,00,00,00,00,00,02,00,00,00,38,e0,3a,00,00,00,00,00
Either apply the file or do it from within KeyTweak and the caps lock key is now an AltGr key. A reboot is probably in order to put it into effect. It is windows, after all.

Keyboard layout for windows

I used a Microsoft tool called Microsoft Keyboard Layout Creator which is a nice tool for creating a new layout from scratch or basing it on an existing layout. It has some limitations however in that you can't remap things like caps lock and the windows key. But on the other hand it generates nice install packages for your keyboard layout. I decided on the following keyboard shortcuts (remember caps lock is now AltGr so it is easily available for the left pinky finger):
	AltGr+a = !
	AltGr+s = #
	AltGr+d = @
	AltGr+f = $
	AltGr+g = (
	AltGr+h = )
	AltGr+j = /
	AltGr+k = \
	AltGr+l = ~
	AltGr+ø = =
	AltGr+æ = +
	AltGr+u = {
	AltGr+i = [
	AltGr+o = ]
	AltGr+p = }

Keyboard layout for linux/ Xorg

When applying the custom layout for windows this worked well for native windows apps, but when logging into the linux servers and X-forwarding apps to the cygwin X server, it naturally didn't work. A similar custom keyboard layout for the X server was needed. I found a great detailed guide on creating keyboad layouts for X11/Xorg with XKB. There are some steps involved:
  1. Copy the layout you want to base you new layout on to a new file in /usr/share/X11/xkb/symbols.
  2. Tweak it to your liking.
  3. Add it to the list of supported keyboard layouts in /usr/share/X11/xkb/rules/xfree86.lst
  4. Restart your X server and use setxkbmap to set it, or add it in xorg.conf
The interesting parts of the symbols file looks like this:
key { [	u,	U,	braceleft,		uparrow ] };
key { [	i,	I, 	bracketleft,	idotless ] };
key { [	o,	O,	bracketright,	Ooblique ] };
This snippet maps e.g. AltGr+u to braceleft which displays a nice left curly brace. The format is broken down like this:
  • The key name AD07 is the 7th (07) alphanumeric (A) key from the left of the fourth row (D) from the bottom. Bottom row is A, second to bottom B and so on.
  • First column is character displayed when the key is pressed normally
  • Second column is character displayed when the key is pressed with shift down
  • Third column is character displayed when AltGr is down
  • Fourth column is character displayed when both AltGr and shift is down
Key names can be found in /usr/X11/include/X11/keysymdef.h The keyboard layout is exactly the same as for windows and I just based it on the standard Norwegian layout and called it progn (pun intented). The entire progn keyboard symbols file is available for download. So now I have remapped the keyboard to my liking in both Linux and windows. I'll probably tweak the keymappings a bit after some use, but generally I'm very happy with them.
References:
blog comments powered by Disqus

Published

22 July 2009