Making use of Extra Keys under Linux
Introduction
Binding typical Functionalities under KDE 3
Making Extra Keys available to X
Sample: Logitech iTouch Keyboard
History
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:
- Make the keys available to X
- 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.
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:
- Start xev from a shell.
This is vital as you need to see the log of xev.
- 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),
state 0x0,
keycode 229 (keysym
0xffcc), same_screen YES,
XLookupString gives 0 characters: ""
xev 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.
- 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.
- 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.
- 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.
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:
- To bind a program to start to a certain key, start the menu
editor. There you can bind a key to any application that is in your
start menu. If you want to start an application that is not yet included
in your start menu when pressing a certain extra key, you will have to
create a respective entry first.
- If you want to trigger certain actions within specific programs,
things are not so easy. If it is not a KDE application, chances are very
bad. If it is a KDE application, look if there is a menu entry to
redefine key bindings. It may be rather hidden at times, see the kmix sample below.
- If you still could not bind a key to your desired action, edit .kde/share/config/khotkeysrc in your
home directory. For example, if you want to open a certain file /tmp/foo using nedit by pressing F13, create a new
entry that looks like this:
[Section2]
MenuEntry=false
Name=whatever
Run=/usr/X11R6/bin/nedit
/tmp/foo
Shortcut=F13
Note that the section number (here: 2) is an enumeration. MenuEntry is false since there
is not a respective menu entry for this action, or you could have used
the menu editor to bind the key, Name
can be whatever you want. Run
is the command to execute (note that you should use absolute path names
when referencing non-KDE applications or files) and Shortcut is the key symbol to
assign the action to.
Here are some samples for common things that you might want to do:
- To lock the screen, use the command
dcop kdesktop
KScreensaverIface lock
- To shut the machine down (logging you off first), use the command
dcop ksmserver ksmserver
logout 0 2 0
- CD player control
play: dcop kscd default play
stop: dcop kscd default stop
next track: dcop kscd default
next
previous track: dcop kscd
default previous
- volume control
mute: dcop kmix Mixer0 setMute 0
1
decrease volume: dcop kmix
Mixer0 decreaseVolume 0
increase volume: dcop kmix
Mixer0 increaseVolume 0
Note: the latter two assume that you are using sound device 0. That
should be fine for most users unless you have multiple sound cards in
your system.
- Another way to bind increasing or decreasing volume or muting:
Start kmix, right-click on the volume symbol (or any other of the
controls) and choose key bindings.
The following dialogue offers the possibility to bind keys to the
respective actions.
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:
[Main]
Num_Sections=11
Version=1
[Section1]
MenuEntry=true
Name=K Menu -
Internet/KMail.desktop
Run=Internet/KMail.desktop
Shortcut=F21
[Section2]
MenuEntry=false
Name=Lock Screen
Run=dcop kdesktop
KScreensaverIface lock
Shortcut=F24
[Section3]
MenuEntry=false
Name=Shut down
Run=dcop ksmserver ksmserver
logout 0 2 0
Shortcut=F13
[Section4]
MenuEntry=false
Name=Start Browser
Run=/opt/MozillaFirebird/MozillaFirebird
Shortcut=F23
[Section5]
MenuEntry=false
Name=CD - play
Run=dcop kscd default play
Shortcut=F17
[Section6]
MenuEntry=false
Name=CD - stop
Run=dcop kscd default stop
Shortcut=F18
[Section7]
MenuEntry=false
Name=CD - previous track
Run=dcop kscd default previous
Shortcut=F19
[Section8]
MenuEntry=false
Name=CD - next track
Run=dcop kscd default next
Shortcut=F20
[Section9]
MenuEntry=false
Name=Volume - mute
Run=dcop kmix Mixer0 setMute 0 1
Shortcut=F14
[Section10]
MenuEntry=false
Name=Volume - decrease
Run=dcop kmix Mixer0
decreaseVolume 0
Shortcut=F15
[Section11]
MenuEntry=false
Name=Volume - increase
Run=dcop kmix Mixer0
increaseVolume 0
Shortcut=F16
21.12.2003 initial version
For feedback or comments, email here.