Thursday, May 22, 2008

Remap Keys in Ubuntu 8.04 (Hardy Heron)

My new Dell Inspiron 1520 keyboard is a little different from my old laptop, and now there's a "menu" key where I'm used to the "delete" key being.

http://www.terminally-incoherent.com/blog/2007/08/02/remapping-the-caps-lock-key/ had a good explanation of how to remap a key, but my case was a little different. Here's what I did:

1. Get the proper key codes

First, I had to find the key-code for my Menu key and my Delete key. At a terminal, type (thanks to Tom for the "| grep keysym" addition to make the output more succint):
> xev | grep keysym

This lets you press keys and get the codes for them. Pressing my Menu and Delete keys, I got a lot of data. Buried in it were the following lines:
keycode 117 (keysym 0xff67, Menu)
keycode 107 (keysym 0xffff, Delete)

Okay, pretty simple. "Menu" and "Delete" are the pertinent bits of data.

2. Make the change

Next, I used VI from a terminal to open ~/.Xmodmap (which didn't exist--I had to create it).
> vi ~/.Xmodmap

(If you don't know VI, I'm sure the regular Text Editor would work just as well.)

In ~/.Xmodmap, I put a single line:
keysym Menu = Delete

Then save the file.

3. Apply the change

Finally, from a terminal, run the following command:
> xmodmap ~/.Xmodmap

11 comments:

Anonymous said...

Why didn't you use xkeycaps? (or, if it's a simpler remapping, the system preferences dialog for the keyboard?)

The Writer said...

I have no basis of knowledge for Ubuntu, so I just used the first solution I could find through Google.

If I have to make another change, I'll check out xkeycaps. However, that seems to hint that it only deals with the Caps Lock key. Is this true?

Also, using the system preferences dialog, I could only find where to choose preset keyboard layouts--not where to remap a single key. Is this possible?

Anonymous said...

Synaptic is probably a better first stop than Google. Just click on the search button and type in some text, and make sure the 'look in' option is set to 'description and name'. Same as google, you'll want to play with the search terms to see if you've exhausted the search space.

For recent distros, if you find a tutorial that has you go to a command line to get stuff done, it's probably a tutorial you should avoid.

I didn't know if the system preferences thing would do what you wanted, but it sure sounds like the xkeycaps will...

From the package description in Synaptic:

manipulate X11 keymaps (for xmodmap) graphically

xkeycaps is a graphical front-end to xmodmap(1). It opens a window that looks
like a keyboard; moving the mouse over a key shows what KeySyms and Modifier
bits that key generates. Clicking on a key simulates KeyPress/KeyRelease
events on the window of your choice. It is possible to change the KeySyms
and Modifiers generated by a key through a mouse-based interface. This
program can also write an input file for xmodmap to recreate your changes in
future sessions.

Anonymous said...

What? Avoid tutorials that teach you the command line stuff? How are you supposed to learn linux? Sure, learn someone's shiny interface until the project dies a couple years from now, and you'll be learning the same task over again. What about remote machines? The ssh suite is a standard, remote X sessions and other graphical candy won't be so widely adopted any time soon. The bottom line? If you learn the command line stuff, you will have broad knowledge that will be useful longer, in more contexts, and on more systems.

If you prefer a graphical app, thats fine, but don't poo poo people away from the shell and text based tools.

Unknown said...

I originally went to xkeycaps to change one key, but it didn't recognize my keyboard. This was a much simpler solution than xkeycaps. Thanks

Anonymous said...

Hi,

Command-Line (CLI) is great for all unix-based things. It is also the most accessible way to do something. If you are needing something fast it is usually the ftastest way in the long run to do it. I don't want to sound conceated, but I should know about accessibility being nearly completely blind and all. I manage friends, family members, and school computers maily via SSH/CLI.

Good Tutorials,
Alex,

Bruce Schaller said...

Thanks! I couldn't get xkeycaps to work for me. Netbooks must have funny keyboards. This was just the ticket. The first time I changed my keyboard, before my reinstall it took me an age. Thanks for the pose.

The Writer said...

No problem! I'm always happy to hear from people who found a tidbit of something useful on this blog.

Tom said...

I tried xkeycaps but it was awfully confusing. This was simple and did the trick.

One tip, to avoid wading through all the results of xev, I piped it through grep, using the following line:

xev | grep keysym

Anonymous said...

Thanks a lot man. You have saved my computer.

Anonymous said...

Thanks a lot man. You have saved my computer.