« OOo 3 and the joys of open source | Main | Microsoft news: layoffs, tax and pay, pay, pay »


I recently changed keyboards. The new one has a variety of specially labelled multimedia keys. I rarely use these things, preferring to program my own shortcuts that will work on any keyboard, but I also like a challenge.

On the new keyboard -- a Logitech Cordless Internet Pro -- the Mute and Volume keys work fine while the Email key starts KMail (I use Thunderbird). The other keys -- Media, Play/Pause, Favorites and WWW do nothing, so let's get them working...

xev
All keys generate keycodes when pressed (and released) so we first need to see what codes the multimedia keys produce. The tool to use for this is xev -- the X event viewer. Install it, run it from a console window, and it'll display all keyboard and mouse events until you close the Event Test window. (Ubuntu users will find it's part of the x11-utils package.)

Here's what I get when releasing my non-functioning WWW key:

KeyRelease event, serial 31, synthetic NO, window 0x3800001,
    root 0x188, subw 0x0, time 34924258, (169,-18), root:(175,11),
    state 0x0, keycode 178 (keysym 0x1008ff2e, XF86WWW), same_screen YES,
    XLookupString gives 0 bytes:
    XFilterEvent returns: False

The keycode is really all the information I need, but also notice the keysym (key symbol). Not every key produces a keysym.



xmodmap
If you want to display and edit keycode-to-keysym mappings, you'll need xmodmap. Install it, (it's part of the x11-xserver-utils package in Ubuntu), and run the following command:

xmodmap -pke > ~/.Xmodmap

This will write all current key mappings to the file .Xmodmap in your /home/username directory. The leading "." makes it a hidden file but you can still access it with a text editor.

Looking at the output file I find

keycode 178 = XF86WWW

which is exactly what xev told me. That's not terribly useful as the key doesn't work anyway, but if it did and I wanted to move its function to a different key, I could do so by simply reassigning the keysym.

To test xmodmap changes right away type;

xmodmap ~/.Xmodmap

Tip: Killing CapsLock

I find the CapsLock key particularly useless. I mean, WHO WANTS TO SHOUT ALL THE TIME? Killing it is simple; just remove its keysym in .Xmodmap. Change

keycode  66 = Caps_Lock

to

keycode  66 =

and you'll never accidentally bump it on again!



xbindkeys
What I really need is something to capture the keycode of the WWW key and associate a program with it. What I really need then is xbindkeys.

Install it, (it's just xbindkeys in Ubuntu), and run the following command to create a default file:

xbindkeys --defaults > ~/.xbindkeysrc

Now take a look at .xbindkeysrc in a text editor. You'll find several examples of the format used at the top of the file. In my case I merely added this to the bottom of the file and saved it.

#Start Firefox when the WWW key is pressed
"firefox"
c:178


To test your settings interactively, type

xbindkeys --nodaemon

(Use Ctrl+C to quit.)

Note that the default examples in .xbindkeysrc reprogram the Ctrl+F, Ctrl+Shift+Q and Ctrl+MiddleMouseButton settings. You might want to comment out those out before you proceed!



Auto-start the new settings
Once you've completed and tested all your multimedia key modifications, you can start them automatically each time you boot by adding the following two lines to the bottom of the .bashrc file in your /home/username directory:

xmodmap ~/.Xmodmap
xbindkeys



<--Previous Hidden Linux      Next Hidden Linux -->


Comments

Ah, thank you very much for this tip. On my computer I mapped CapsLock as Alt without a problem but on the other computer I had really hard time mapping it as F13. Your tip helped, thank you.

This is a useful tutorial.
Thanks a lot.

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

Subscribe
Newsletter & SubscriptionsPC World is New Zealand’s top selling computing and technology magazine.

It provides up-to-the-minute editorial, insight and buying advice for personal computing, cell phones, game consoles, digital entertainment and broadband.
SIGN UP
PCWorldUpdate
PC World's weekly round-up of tech news, gear and game reviews, software selections, and handy How Tos.