Making use of Extra Keys under Linux

Binding typical Functionalities under KDE 3
Making Extra Keys available to X
Sample: Logitech iTouch Keyboard


These days it is virtually impossible to buy a keyboard that comes without lots of extra keys for all kinds of purposes. Unfortunately, most of them include only drivers for Windows, while Linux isn't even mentioned. Fortunately, it is not very hard to get most of these special keys to work.
To make use of the extra keys, you have to complete the following two stps:
  1. Make the keys available to X
  2. Bind functionality to the keys
While step 1 is rather generic, step 2 depends upon the desktop you are using. We will give instructions for binding typical functionalities under KDE only.

Making Extra Keys available to X

X needs the keycode to be able to assign an internal symbol to a key. Since that is a very basic step, X comes with small test program called xev that you can use to find out the keycodes of your extra keys. To do so, follow these steps:
  1. Start xev from a shell. This is vital as you need to see the log of xev.
  2. Now press one of your extra keys. The log will show you output like this:

    KeyPress event, serial 27, synthetic NO, window 0x3a00001,

        root 0x36, subw 0x0, time 225375492, (25,175), root:(983,193),
        state 0x0, keycode 229 (keysym 0xffcc), same_screen YES,
        XLookupString gives 0 characters:  ""

    KeyRelease event, serial 27, synthetic NO, window 0x3a00001,
        root 0x36, subw 0x0, time 225375577, (25,175), root:(983,193),
        XLookupString gives 0 characters:  ""

    will log literally every event, hence the first section corresponds to pressing the key, the second section to key release. The essential part here is the keycode, highlighted in blue. For this key, the respective keycode is 229.
    Repeat this process for all your extra keys and make notes of those keycodes.
    Note that not all extra keys can be put to use. Some keyboards and also keyboards of laptops have extra keys that need dedicated drivers to be able to make use of them. You will realize this when you press one of those keys and see no output in the xev log.
  3. Now we have to tell X that these keys exits and how to use them. To do so, we have to set up a modmap for X. In most cases, there will already exist some files, usually .Xmodmap in you home directory and/or /etc/X11/Xmodmap. These are simple text files that can be loaded with a program called xmodmap. For each of your extra keys, you have to make an entry in one of these files that looks like this:

    keycode 229 = F13

    229 is the keycode that we found out in the previous step. F13 is an internal label that we assign to the key. It could be rzlpfrpf or anything else, but in most cases it will make sense to continue enumerating the extra keys as function keys.
    Depending upon whether you want to make these keys available to all users or just an individual user, /etc/X11/Xmodmap or .Xmodmap in the home directory is the right place for these keycode entries.
  4. Now it is time to test you new modmap. Load it via xmodmap <filename>. If there are any syntax errors, you will know by now. To doublecheck your new modmap, issue xmodmap -pk. That will print all keycodes and their assgnments side by side. For our example, you would have to look for the line corresponding to keycode 229. It should look like this:

        229         0xffcc (F13)

    The hex code  0xffcc may vary, the essential part is that F13 has been assigned to keycode 229.
  5. Do not forget to make sure that you modmap file is really loaded when X starts! Simply restart X, log in and use xmodmap -pk to verify that your modmap was loaded.
That's all, what is left to do now is to bind useful functionality to these keys.

Binding typical Functionalities under KDE 3

There are several possibilities to get started here. The very first check should be to in Control Center -> Region Settings -> Keyboard Layout. Check the keyboard types entry, if you are lucky, you will find your keyboard listed there.
If not, you will have to assign functionalities manually:
Here are some samples for common things that you might want to do:

Sample: Logitech iTouch Keyboard

Here are bindings for the Logitech iTouch Keyboard connected via PS/2:

keycode 223 = F13
keycode 160 = F14
keycode 174 = F15
keycode 176 = F16
keycode 162 = F17
keycode 164 = F18
keycode 144 = F19
keycode 153 = F20
keycode 236 = F21
keycode 229 = F22
keycode 178 = F23
keycode 230 = F24

Note that these apparently change when connected via USB.
Once you have added these to your .Xmodmap, the extra keys on your keyboard are available as keys F13-F24 in applications like KDE.

Here is a sample khotkeysrc to make use of the keys:


Name=K Menu - Internet/KMail.desktop

Name=Lock Screen
Run=dcop kdesktop KScreensaverIface lock

Name=Shut down
Run=dcop ksmserver ksmserver logout 0 2 0

Name=Start Browser

Name=CD - play
Run=dcop kscd default play

Name=CD - stop
Run=dcop kscd default stop

Name=CD - previous track
Run=dcop kscd default previous

Name=CD - next track
Run=dcop kscd default next

Name=Volume - mute
Run=dcop kmix Mixer0 setMute 0 1

Name=Volume - decrease
Run=dcop kmix Mixer0 decreaseVolume 0

Name=Volume - increase
Run=dcop kmix Mixer0 increaseVolume 0


21.12.2003 initial version

For feedback or comments, email here.

Back to main page