16 Feb 2006 hisham   » (Apprentice)

How to make any key become any other key in X

The vast majority of humanity does not use computers. From those who use computers, the vast majority does not use X (which is both a good and a bad thing). From those who use X, the vast majority will never have or want to tweak the settings of individual keys on their keyboard. But for those who do... here's how.

X has two nifty little programs: xev and xmodmap. The first tells you what is a key doing and the second allows you to tell it to do something else.

Say you want shift-4 to do pound signs instead of dollar signs. Run xev from a terminal. It will open a little window. Xev is an event dumper: whenever you do anything with that window (pass the mouse over it, move it around and, of course, press keys), xev will tell you what exactly the window is doing. For shift-4, xev reports:

KeyPress event, serial 30, synthetic NO, window 0x400001, root 0x60, subw 0x0, time 711922155, (165,-9), root:(171,43), state 0x11, keycode 13 (keysym 0x24, dollar), same_screen YES, XLookupString gives 1 bytes: (24) "$" XmbLookupString gives 1 bytes: (24) "$" XFilterEvent returns: False

KeyRelease event, serial 30, synthetic NO, window 0x400001, root 0x60, subw 0x0, time 711922297, (165,-9), root:(171,43), state 0x11, keycode 13 (keysym 0x24, dollar), same_screen YES, XLookupString gives 1 bytes: (24) "$"

I put the key data in bold. With xmodmap, I can now do this:

xmodmap -e 'keycode 13 = 4 sterling'

And there you go: £10.25! The first entry, '4' stands for the standard key, the second entry, 'sterling' for the shifted key. Amusingly, I forgot to put the '4' the first time around and had to resort to the numeric keypad to get it back there when running the command again (so watch out not to bork your keyboard when messing with xmodmap).

To figure out the names of the keys, take a look at include/X11/keysymdef.h in your X distribution. Or just use the Tab key if you're a zsh user like me.

I configured my right Alt key (keycode 113) to be "Mode_switch". This is the magic key that allows you to give more than 2 signs to a key. Doing this:

xmodmap -e 'keycode 13 = 4 dollar sterling yen'

I can now type: £ 1.00 = US$ 1.74 = ¥ 203.81 = R$ 3,72. (Actually I don't know whether the UK and Japan use dots or commas.)

I used xmodmap extensively to make an internationalized version of Dvorak for my own use (standard Dvorak + accents on strategic Mode_shift positions). Xmodmap tricks are also great for disabling the Caps Lock key, or to turn it into an extra Control key (one can never have too many of those).

[Ok, and now I promise I'll get back to working on my dissertation proposal!]

Latest blog entries     Older blog entries

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!