Keyboard

Revision / Modified: June 12, 2002
Author: Tom Berger

Original documents:
http://www.mandrakeuser.org/docs/xwin/xkeys.html
http://www.mandrakeuser.org/docs/xwin/xkeys2.html

Remapping Keys

An X key map acts as an interpreter between your hardware (the keyboard) and the software (the applications running on X). It is compiled every time you start X. Three options in the 'Keyboard Section' of '/etc/X11/XF86Config-4' determine how it looks like:

Option "XkbRules" "rule"
Option "XkbModel" "model"
Option "XkbLayout" "layout"

The available rules, models and layouts are stored in the respective subdirectories of '/usr/X11R6/lib/X11/xkb'. 'Rules' define key behavior on different hardware platforms (roughly speaking), 'Models' refer to the keyboard model, and 'Layouts' usually define the language mapping of the keyboard. Good news is that this is only of theoretical interest *grin*.

To see what your current X key map looks like, run

xmodmap -pke | more

A typical 'xmodmap' line looks like this:

keycode 10 = 1 exclam onesuperior exclamdown

This is the mapping for the '1' key on a German keyboard. The first value denotes the 'keycode', a shorthand reference and a very practical thing when it comes to remapping it. The names on the left tell you which 'events' are currently mapped to this key.
The first 'event' is what happens when you simply press the key. The other values tell you (and X) what happens when you use a modifier key in combination with this key: 'SHIFT+1' results in an exclamation mark, the second modifier key (usually that's 'Alt Gr') and '1' result in ¹ and a third modifier key in combination with '1' would provide the ¡, the Spanish exclamation mark.

Notice that 'SHIFT' is regarded as a 'natural' modifier, so when you run

xmodmap -pm

to show the list of modifier keys, the second modifier key is listed as 'mod1'.

To change a key-mapping from the command line, all you need to do is:

xmodmap -e 'keycode keycode=mapping'

Let's say you're a 'vim' freak. 'vim' is an editor and uses the ESC key a lot. Regrettably this key is somewhat inconveniently located. How about switching the position of the CapsLock key with the ESC key?

xmodmap -pke | egrep -i '(caps|esc)'

provides us with the necessary keycodes and the exact value names:

keycode 9 = Escape
keycode 66 = Caps_Lock

Now switch:

xmodmap -e 'keycode 9=Caps_Lock' && xmodmap -e 'keycode 66=Escape'

You should now be able to get the ESC event by pressing the now-former CapsLock key.

These changes however will be lost at the end of your X session. If you want them to be permanent, create a file in your home directory called '.Xmodmap' and put them in there like this:

keycode 9 = Caps_Lock
keycode 66 = Escape

Now all that's left to do is create a script and let it run automatically every time you start X.

Now you might ask: 'Isn't there a more comfortable way to do this? Maybe some graphical tool?' Indeed, there is, and it's called XKeyCaps ( RH RPM).
It's old, it's ugly, but it does its job. Note that XKeyCaps does not auto-detect your keyboard settings, you'll have to specify them in the first pop-up window dialog. Furthermore it doesn't change your current key map, all it does is writing the changes you made to an '.Xmodmap-host' file ('Write Output').

Mapping New Keys

The method introduced above only works for keys which have been mapped by X already. But what if X doesn't even seem to recognize the keys, like those 'Multimedia keys' on some newer keyboards?

Enter 'xev', the 'X Event Handler'. 'xev' is part of the X11R6-contrib package which you will likely have to install first. Start 'xev' from a command line window. You will immediately note lots of output in the window every time you press a key or move the mouse in the 'xev' window. The 'xev' output looks a bit cryptic:

KeyPress event, serial 27, synthetic NO, window 0x2c00001,
    root 0x2d, subw 0x0, time 1556344064, (8,-8), root:(12,702),
    state 0x10, keycode 36 (keysym 0xff0d, Return), same_screen YES,
    XLookupString gives 1 characters:  "

You can discard most of it, however. For you, only this part is interesting:

keycode 36 (keysym 0xff0d, Return)

This tells you all you need to know: the keycode, the keysym and if and how that key is mapped. If you get no output, you can forget using that key in Linux.
As you can see, I pressed the 'Return' button in this example. Now let's see what I get if I press an unmapped button:

KeyPress event, serial 27, synthetic NO, window 0x2c00001,
    root 0x2d, subw 0x0, time 1556704161, (10,-10), root:(14,700),
    state 0x10, keycode 234 (keysym 0x0, NoSymbol), same_screen YES,
    XLookupString gives 0 characters:  ""

This key (it's the 'browser back' key on a Microsoft Internet Pro keyboard) has a keycode, which is good, but does not have a keysym or a mapping ('NoSymbol'). If you now do something like

xmodmap -e 'keycode 234=e'

pressing this key now returns the letter 'e'. Of course that isn't very useful. What we need is a row of unused, valid values you can map those keys to ... '/usr/X11R6/lib/X11/XKeysymDB' might just be what you need. Lines 200ff look very promising. OK, now we've got a list of valid names to map the keys to. Use 'xev' to find out about the other keycodes and then create an '~/.Xmodmap' file to map them to the names you'll find in the 'XKeysymDB' file.

Once you've done that and either sourced the file with 'xmodmap' or created an automatic startup script and restarted X, you should be able to use these keys in the key binding configuration utility of your window manager or environment.

Mandrake Linux user Braden MacDonald suggests that "instead of mapping them to whatever is on line 200ff of XKeysymDB, it is a lot easier to just map them to F14, F15, F16, F17, etc."

Mandrake Linux user Linegod has come up with yet another way to configure these keys for KDE.

If you prefer a graphical utility, get lineakd and lineakconf. Install both packages, run

lineakd -l

to list all available keyboards and their short codes, and then

lineakd -c your_keyboard_short_code

to create the basic configuration file in '~/.lineak'.

Now you can start 'Lineakconfig' via the menu ('Configuration - Hardware') and configure your shortcuts. In order for those to work, 'lineakd' must be running in the background ('-b' option).

Using The 'Windows' Keys

Most window managers and environments allow using the 'WinKeys' as modifiers (that is in combination with other keys) to configure keyboard shortcuts for all kinds of actions, and there is no further modification necessary.

If the configuration utility for your window manager or environment does not seem to recognize these keys, you should first check the key map with:

xmodmap -pk | grep 11[567]

115, 116 and 117 are the usual keycodes for the 'windows keys' (from left to right). Unless you are running KDE, you should get something like this:

115         0xffe7 (Meta_L)
116         0xff20 (Multi_key)
117         0xff67 (Menu)

If the right hand column is empty, check '/etc/X11/XF86Config-4' (as 'root') for the XkbModel entry. This entry should be set to either pc105 or microsoft.

If everything's correct and it still doesn't work (or not the way you want to), you have to set the bindings by hand, don't worry, it's not that difficult.
This can be done via the 'xmodmap' command. Create a file in your home directory called '.Xmodmap'. The most simple case for the Windows keys would be mapping them to function keys. To avoid conflicts with existing mappings, use F13, F14 and F15:

keycode 115=F13
keycode 116=F14
keycode 117=F15

Save this file and run

xmodmap .Xmodmap

Now return to your manager's shortcut configuration utility. Using the provided 'grab' function, press the keys, and you should see the appropriate mapping appearing in the utility.

Mandrake Linux seems to have tried to achieve a similar thing, first via patching '/usr/bin/startkde' directly and in 8.1 and later with the '/usr/bin/test-windows-key' script and the 'DISABLE_WINDOWS_KEY' option in '/etc/sysconfig/keyboard'. Their solution however does not work for me:

  1. The right 'flag' key isn't mapped.
  2. The mapping of the menu key (117) to 'menu' in 8.1 and later confuses KDE and GNOME (at least with a German key map): the left 'flag' key (115) now appears to be the menu key, whereas the menu key is blocked. So I'm left with effectively one additional key ...

As for the question if you can use the 'flag' keys as modifier keys (i.e. like in Windows in key combinations): of course you can :-). The appropriate mapping in '~/.Xmodmap' looks like this:

keycode 115 = Meta_L
add mod4 = Meta_L
keycode 116 = Meta_R
add mod4 =Meta_R

Et voila, another modifier key! Using these keys as modifiers has a huge advantage: no Linux application uses them by default. This means you are totally free to create shortcuts using these keys without having to be afraid to collide with preset application shortcuts. And yes, that's the setup I'm using ;-).

Keyboard Utilities

Here are some utilities for keyboard handling in Linux. Maybe you find something of interest to you.

Binding keys

xbindkeys allows you to bind key combination and mouse movements to shell commands. Most window managers and desktop environments offer their own key binding utilities, but 'xbindkeys' is window manager independent and also captures 'extra' keys on 'Internet keyboards' and such. RPM is available from the website.
Make sure to also get the graphical configuration utility, XBindKeys_Config (RPM available from website.

Touch-Typing Tutors

Several programs allowing you to train typing without having to look at the keyboard are available:

Read the review on 'Linux Journal' by Marcel Gagne for in depth information.

Control Keyboard LEDs

Most keyboards feature status LEDs for certain key functions, usually 'ScrollLock', 'ShiftLock' and 'NumLock'. You can use these LEDs for displaying various bits of system information like incoming e-mail, network traffic, system load and what not. You can then use LED status monitors to display those blinking LEDs on your desktop ... ;-)

Two of them are:

Freshmeat has more.

On-screen Keyboards

xvkbd and GTKeyboard display a virtual keyboard on the screen for people who can't or don't want to use the keyboard for character input. 'GTKeyboard' is included in Mandrake Linux 8.2 (package 'gtkeyboard').

Remote Control

Utilities like xremote allow you to take over the input devices of a remote machine. RPM is available from the website.


Legal: This text is covered by the GNU Free Documentation License. Standard disclaimers of warranty apply. Copyright LSTB (Tom Berger) and Mandrakesoft 1999-2002.