Make special keys Just Work · Aug 4, 17:49 von stargaming
I’m using a “Wireless Desktop Edge” from Labtec (image, large). I haven’t cared about the special keys so far (after migrating to Linux), but after a while the mouse became really inconvenient.
Notice: I’m doing this under Sidux (Linux 2.6.22.1-slh-smp-2 #1 SMP PREEMPT Wed Jul 11 01:45:29 CEST 2007 i686 GNU/Linux), KDE-full. YMMV.
Getting keycodes
Retrieving a keycode to a button on the keyboard is pretty trivial. Start the application xev and press whatever button you’d like to know about. Run it in a terminal because the information will go to the console. Close the window when you’re done (or press Ctrl+C).
Along those lines, the latter ones will be the most interesting ones for you (this is the event for my shutdown/hibernate key):
KeyPress event, serial 29, synthetic NO, window 0x3600001,
root 0x1a5, subw 0x3600002, time 826747352, (51,47), root:(55,831),
state 0x0, keycode 223 (keysym 0x0, NoSymbol), same_screen YES,
XLookupString gives 0 bytes:
XmbLookupString gives 0 bytes:
XFilterEvent returns: False
The keycode is in line 3, if you’ve overseen it.
If you’re too lazy to do this yourself, I can give you my results:
| key | keycode |
|---|---|
| Play/Pause | 162 |
| Stop | 164 |
| Previous | 144 |
| Next | 153 |
| Mute | 160 |
Linking the keycode to a symbolic name
In this step, we’ll utilize the xmodmap tool. There is a good howto from David Vespe, Remapping Keys, but the manual page should be enough for everyone.
For a quick introduction, there is that keycode and there is a “keysym”, a symbolic name for the key. Basically, you can use any symbolic name defined in /usr/include/X11/keymapdef.h (without the KX_ prefix). I found this group symbolic key names for the 3270 terminal and since it wasn’t in use, I took that one. You could overwrite the “Japanese Keyboard Support”, “Braille” or “Vietnamese” as well, I guess.
#define XK_3270_Quit 0xfd09
#define XK_3270_Jump 0xfd12
#define XK_3270_Play 0xfd16
#define XK_3270_DeleteWord 0xfd1a
#define XK_3270_ExSelect 0xfd1b
I took those because I found these names most… symbolic for the keys. ;)
| key | keycode | keysym |
|---|---|---|
| Play/Pause | 162 | 3270_Play |
| Stop | 164 | 3270_Quit |
| Previous | 144 | 3270_ExSelect |
| Next | 153 | 3270_Jump |
| Mute | 160 | 3270_DeleteWord |
You can try mapping keycode to keysym with xmodmap -e, as in:
$ xmodmap -e "keycode 162 = 3270_Play"
$ xmodmap -e "keycode 164 = 3270_Quit"
$ xmodmap -e "keycode 144 = 3270_ExSelect"
$ xmodmap -e "keycode 153 = 3270_Jump"
$ xmodmap -e "keycode 160 = 3270_DeleteWord"
If you go to any KDE application that supports “Global Shortcuts” now, you’ll actually find a reaction to those keys.
Mapping permanently
Now, there are several ways to run those xmodmap commands once per session. But the most convenient is, in my opionion, modifying /etc/X11/Xsession/ to invoke xmodmap ~/.xmodmap at the start of an X session.
XMODMAPFILE=$HOME/.xmodmap
if [ -f $XMODMAPFILE ]; then
xmodmap $XMODMAPFILE
fi
Now, this will source the .xmodmap file in your home directory. You can just fill this with the arguments you gave to xmodmap -e.
keycode 162 = 3270_Play
keycode 164 = 3270_Quit
keycode 144 = 3270_ExSelect
keycode 153 = 3270_Jump
keycode 160 = 3270_DeleteWord
Alternative mapping
If you haven’t got root access, you can perhaps just edit your .Xsession, .xsession or .loginrc (first two confirmed in my /etc/X11/Xsession, latter one from a 10 years old internet source). The line xmodmap $HOME/.xmodmap should be enough in there.
KDE startup manager (updated)
I might have been kinda mislead about the persistance/invokation on KDE. It seems you rather have to edit /etc/kde*/kdm/Xsession. If you cannot edit this document, a file based on your shell might work, too:
| shell | location |
|---|---|
| bash 1 | $HOME/.bash_profile $HOME/.bash_login $HOME/.profile |
| zsh 2 | $zdir/zprofile $zhome/.zprofile $zdir/zlogin $zhome/.zlogin |
| csh/tcsh | /etc/csh.login ~/.login |
| * | /etc/profile $HOME/.profile |
1: only the first existing file in this list is sourced.
2: $zdir is /etc/zsh or /etc, $zhome is ${ZDOTDIR:-$HOME}
Your shell will be determined by your $SHELL variable.

