Getting Multimedia Keys on keyboards working in Linux


Have you looked on with envy as your Windows using friends have pressed one of those multimedia buttons on their keyboard to launch their email client or Messenger? Got one of those keyboards yourself - but the driver only supports Windows?

Well, now its time to show the Windows users that we too can play with multimedia buttons - and without installing drivers.

Ok - this may not look particularly easy - but I promise it is! Just take your time - the hardest bit is writing down all the numbers you will collect.

First thing we need to do is to discover the keycodes of all those multimedia buttons. This will involve a command line - sorry! But its easy!

So, open your Konsole up (the command line terminal), and type in -
xev <press enter>

That should bring up a little white box with a smaller box inside. Notice that if you place your mouse cursor into the box, it will start putting readings into your terminal - not of use here, but interesting

Right, now get hold of a pencil and paper.
Press one of your multimedia keys - for instance, the email one (I will use this as an example).
When you press the key, it should give you some information in the terminal like this -

KeyRelease event, serial 26, synthetic NO, window 0x2a00001,
 root 0x91, subw 0x0, time 2147964, (310,723), root:(314,745),
 state 0x0, keycode 36 (keysym 0xff0d, Return), same_screen YES,
" XLookupString gives 1 bytes: (0d) "

A right load of gobblydegook - but the important part is in there - the bit mentioning keycode (in this case 36).
So, note down the keycode number on your paper next to email or whatever.
Follow the same procedure for the other multimedia keys, noting down the keycode each time next to the name of the key. Don't do this for the normal keys - they are already set up

Ok, you should now have a list of numbers corresponding to the keys on your multimedia board. You can exit the xev program now by clicking the x in the small white window. This will stop xev, but leave the termial open for the next part.

Now its time to get these keycodes into a useable form. Again, this will involve the terminal. From your home directory in the terminal (if not sure - simply type cd and press enter to get to your home directory), type -

cd .kde < press enter> (this takes you to the hidden kde directory in your home)

cd Autostart <press enter >

You are now in the Autostart directory - where KDE will automatically run whatever is in there - which will be useful for the keys!

Now, still in the terminal at Autostart, lets open up our favourite editor. I use nano - its a lot easier than vi!

so type in -

nano keycodes < press enter > (this will create a new text file called keycodes)

Now its time to dig out the paper with those numbers on it!

We want the keycodes assigned to none-existant keys now. You could in theory use anything, but the most common way to do this is to assign them to F keys. As you will already have some of these on your keyboard, try assigning to F keys above F20.
Seems a bit confusing, but hopefully the example below will explain-
 
Type into the editor-

#!/bin/sh
xmodmap -e "keycode 36=F21"
#email client

xmodmap -e "keycode 171=F22"
#Messenger

and so on.

Enter your own keycodes from your paper, and simply set them to an F key. It doesn't really matter what you set them to - as long as the key doesn't exist! Using F20 and up is probably the simplest.
The hashed out comments (#email client etc) are purely for helping you remember which key is which - but it isn't actually necessary.

Save the file as keycodes and exit the editor (in nano, that is ctrl+x, press y to save as keycodes).

Now we need to make the file executable, so still in the Autostart directory, type -
chmod +x keycodes <press enter>
./keycodes <press enter>

That will make the file executable and start it up (note that you won't see anything happen).
From now on, each time KDE starts, the keycodes script will run - because we put it into the KDE Autostart directory and made it executable.

I am sure you will be pleased to hear that we can now close the terminal!
Not quite finished yet with the key mapping - but its all working from your desktop now

This is the easy bit - open the KDE control panel (from either the taskbar shortcut or the control centre entry in your "start" menu). Open the Regional and Accessability option. Then select keyboard shortcuts.
At the top of the keyboard shortcuts, you will see a tab entitled Command shortcuts.
Click that.
This bit is really rather cool - using my example of email, look through the available options (rather like your start menu) - so for instance, mail is going to be in the Internet section - so click the plus sign next to Internet.
Simply select the Kmail ( or whatever you prefer ), then click the Custom radio button at the bottom - this will launch a window called Configure Shortcut. This is the best bit - no need to type anything technical here, simply press the key you have already set up ( i.e the multimedia Email key) and the program should fill in F21 (or whatever you selected)for you and close. Thats it - one key assigned and fully set up!
Feel free to try it - press the email button on your keyboard and watch kmail come up!
Once you have seen it work - its simply a case of finding the program you wish to associate with each key and repeating this small process.