Swapping keys with Linux and xmodmap

We’ve had all kinds of Graydon goodness, but I’ve finally solved a pesky problem that is worth documenting for my future reference. My laptop does not have a right CTRL key. I didn’t realize how much I used the right CTRL key until I didn’t have one. I’ve finally got it worked out, which makes using Emacs much better on my Ubuntu laptop…

My Toshiba laptop does not have a right CTRL key. What I wanted to do was to swap the right ALT key for the CTRL key. Here is how to do that in a few easy steps. I’m using Ubuntu 7.10 on a Toshiba Satellite laptop, but the method should translate to other setups easy enough.

1) Figure out what key the computer thinks the right ALT key is. Do this by running “xev” in a terminal window. The xev program opens a small test window that reports everything that happens- mouse movements, key presses and releases, etc. Pressing the right ALT key gives me this:

KeyPress event, serial 31, synthetic NO, window 0x3200001,
root 0x58, subw 0x3200002, time 3826291679, (28,18), root:(702,69),
state 0x0, keycode 113 (keysym 0xffe4, Alt_R), same_screen YES,
XKeysymToKeycode returns keycode: 109
XLookupString gives 0 bytes:
XmbLookupString gives 0 bytes:
XFilterEvent returns: False


The important part to note here is this bit: “keycode 113 (keysym 0xffe4, Alt_R)”. I want to change Alt_R to be Control_R.

2) I created a file in my home directory called .Xmodmap.

! swap the Right ALT key to be a CTRL key
keycode 113 = Control_R
add control = Control_R


Brilliant in its simplicity, n’est pas?

Line 1: “! swap the Right ALT key to be a CTRL key
– All lines that start with ! are comments. Comments are good, because I will forget what I was trying to do six months or so from now when I take a second look at this code.
Line 2: “keycode 113 = Control_R
– Assign the right ALT key, which we identified as having keycode 113, to be Control_R, ie, the right side CTRL key.
Line 3: “add control = Control_R
– Assigns the Control_R key to be part of the ‘control’ modifier group. This was the hard part- I could get the key swapped easy enough, but it took some trial and error to figure out that it needed to be added to the modifier group.

3) Upon logging out and logging back in, Ubuntu detected the presence of my .Xmodmap file in my home directory and popped up a dialogue box asking me if I wanted to load the map. I confirmed that I did and checked the “do not ask me again” box and everything seems to be working great, including inside Emacs, which, for me, was the whole driver in the first place.

Need more help? Here’s where I got most of my information:
[xfree86.org’s man page for xmodmap]
[cweiske.de’s howto page for xmodmap]

This entry was posted in computers/programming. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *