Using the extra keys of a so-called multimedia keyboard on Linux

Making sure they are enabled in the kernel

The first step is to make sure that all your new keys are enabled in the lowest kernel layers. To do that, start xev in your X session from a XTerm and press the various special keys while the xev window has the focus. The enabled keys will generate four lines of text when pressed or released. If the xterm stays silent for some of them, then read on.

Watch your kernel log (tail -f /var/log/kern.log or as applicable on your system - at worse, use dmesg). Upon pressing the keys that didn't produce any xevent earlier, you should see something similar to

atkbd.c: Unknown key released (translated set 2, code 0xa2 on isa0060/serio0).
atkbd.c: Use 'setkeycodes e022 <keycode>' to make it known.

Just do as it says, however you have to determine the available keycodes first.

The manual page says that you may use the 0-127 range, however it might be outdated and the 128-256 range might be valid too. You don't want, however, to use keycodes that are already assigned to other keys. Typically, the 89-95 and 120-127 ranges are still free, however you can check that using dumpkeys --keys-only|grep =\\$ as root (it lists only the unassigned keycodes, thanks to the grep).

You can now do the keycode assignment itself for every of the "missing" keys. For example, setkeycodes e022 89 would match the log entry above. Last but not the least, do not forget to integrate these commands to your system startup scripts, else these settings will be lost on reboot.

Making use of these keys inside X11 - hotkeys

So, you've got all the keys working, they can now be used inside X11 (it is also possible to use them in console mode, for that you'll have to customize your kernel keymap - check man loadkeys). It now mostly depends on what you want to use them for.

In this paragraph I'll assume that you want high-level actions associated with (some of) your keys, such as command execution. There is a nice little program simply called hotkeys that does that. Once you have installed it, first check if by chance your keyboard is in the presets (hotkeys -l). In the case it is not, or that some keys are not working (because you had to assign them as shown in the previous paragraph), you'll have to create or modify a configuration file. It is a really straight-forward operation. I won't rewrite here what is already in the manpage, only the missing bit: getting the key codes of the X events.

Fire up xev once again, and press the keys you want the keycodes of. The output will be something like:

KeyPress event, serial 26, synthetic NO, window 0x2a00002,
    root 0xa0, subw 0x0, time 1077835786, (298,456), root:(565,545),
    state 0x0, keycode 122 (keysym 0x0, NoSymbol), same_screen YES,
    XLookupString gives 0 bytes:  ""

KeyRelease event, serial 26, synthetic NO, window 0x2a00002,
    root 0xa0, subw 0x0, time 1077835786, (298,456), root:(565,545),
    state 0x0, keycode 122 (keysym 0x0, NoSymbol), same_screen YES,
    XLookupString gives 0 bytes:  ""

Four lines for the key press, four more lines for the key release. The interesting bit is keycode 122. 122 is then the value you'll use for this particular key in your hotkeys configuration.

Last bit: do not forget to include it in your X startup config. For example, if you use a ~/.xinitrc, just add a hotkeys -t yourconfigname somewhere before the final exec yourwindowmanager.

Making use of these keys inside X11 - xmodmap

In this part I'll address the case where you want to use a part of your "multimedia" keys for more useful purposes (who needs browser control on the keyboard, anyway?). xmodmap basically lets you map any keyboard event to keys (as seen by the X clients).

I chose to simply map my new keys as Fxx keys (xx up to 35), but it's your configuration, so pick up your favourites in keysymdef.h (locate it, on my Debian it was /usr/X11R6/include/X11/keysymdef.h). Do remove the XK_ prefix, however.

Somewhere in your X startup config (similar to the hotkeys startup), add xmodmap ~/.xkeys. In this file (or any other that you specified), just add: keycode 174 = F13 for every new key. 174 being, of course, what xev reported (see the previous paragraph).

After applying it (with the xmodmap command), all the X clients ("programs") should recognize these new keys. The function keys I chose have the additional feature of being recognized by most xterms, that translate them into a vaguely standard ANSI code.

Additional XTerm trick

If you are using the original XTerm, it is even possible to translate again key events when they reach XTerm, into string sequences for example.

BuD wrote a sample xresource file that translates F1-F12 into easy-to-catch key sequences, and also works for modifiers (Control, Alt). Extremly useful to play MUDs ;-)

Sanitizing your keyboard layout

The default (as in the printed one) PC keyboard layout for the special keys sucks. It might be ok for the average luser (who'd use MS Word without his keyboard if it was possible), but it is definitively wrong for the Vim user who wishes he had an escape pedal, or the Emacs one who wears out three control/alt keys a year. I'll include here sample config snippets, please consult the xmodmap manpage for the complete reference

The Right Position of the control key is on the third row, right where they placed the caps lock one. Placing it on the lowest row forces you to bend your wrist to reach it with your little finger, guaranteeing you a RSI sooner or later. It was correctly placed "back then", and X11 allows us to reproduce this setting, by swapping the left control key and the caps lock one (feed xmodmap with this, see the related paragraph above):

! Swap the "Caps Lock" and left "Control" keys.
remove Lock = Caps_Lock
remove Control = Control_L
keysym Control_L = Caps_Lock
keysym Caps_Lock = Control_L
add Lock = Caps_Lock
add Control = Control_L

Likewise, the escape is misplaced, especially for the vim users. Being on the very top row with the function keys, it forces you to interrupt your typing and completely move your left hand in order to reach it. I find it much more handy when duplicated on the left neighbour of the "1" key. On my keyboard, this key originally gave a ² unshifted and a ³ when shifted. I decided to make it an escape key unshifted and have the ² upon use with AltGr. The keycode may be different on your system, use xev to figure it out.

! The key to the left of 1, (²/³) becomes ESC/³/²
keycode 49 = Escape threesuperior NoSymbol NoSymbol twosuperior