Configuring keyboards

From LQWiki
Jump to: navigation, search

Configuring Your Keyboard Layout

Different countries have different keyboard layouts. For example a german keyboard, when compared with an english one, exchanges the "y" and the "z"-key. However just connecting a german keyboard will still give you a "y" when you press the key labeled "z". You have to configure your keyboard layout on the software side. This layout is also called keymap. Here is how you can achieve this.

Using the Desktop

For KDE, start kcontrol (also called Control Center) and chose Regional & Accessibility -> Keyboard Layouts.

With Gnome, up to three extra maps may be selected as well as the default. If the Keyboard Indicator is added to the panel, this will show the current map and enable it to be changed with a mouse-click.

The Loadkeys Method

This method changes the keyboard driver's translation tables, but as X does not consult these tables, it will have no affect on X or programs running under it.

If you merely want to switch to a new keyboard layout, the command loadkeys mapname will probably work. If mapname cannot be found, the map you want will probably be somewhere under the /usr/lib/kbd/keymaps directory hierarchy - variations on the dvorak layout, for instance, may be found in the /usr/lib/kbd/keymaps/i386/dvorak/ directory. Maps generally have the .kmap suffix, and they may have been compressed by gzip. When you think you have found the map you want, take the base name and pass it to loadkeys. You can also give a full path.

If there is no keymap that you like, you will have to create your own. This is easier if you start with a similar one, so find a map that is close to the one you want. Decompress it if necessary (zcat file > newname will keep the original) and edit it in a text editor. When done, save your work and pass it to loadkeys. If there are any errors, it will not change your keymap, so sort them out and try again. Note that it is case sensitive - "BackSpace" is a valid keysym, whereas "backspace" is not.

The syntax can be found with man 5 keymaps. If you wish to know the keycode generated by a particular key, the showkey command will tell you (it may not work properly or at all in X). A list of valid keysyms can be obtained with dumpkeys -l, or at List of keysyms.

The Xmodmap/Setxkbmap Method

This method changes how X handles the keycodes it receives, and as such will only have an effect whilst in X.

If you simply want to switch layouts, setxkbmap is the program you want, e.g.

setxkbmap -layout us

switches to the us keyboard layout. To search for available maps, start the KDE control center (kcontrol) and chose Regional & Accessibility => Keyboard Layout. You can also use different variants of a keymap here, e.g. the dvorak variant:

setxkbmap us -variant dvorak

If no "-variant" is specified, the default is "-variant basic".

In essence, setxkbmap is more portable in that it has a modular nature and does not use raw keycodes. Instead, it uses its own codes which it then converts into the raw keycodes. Since the tables saying how this is done are separate from those saying what each key should do, a keymap can be used with many different keyboards.

The easier way to do this is to use xmodmap. Small changes can be made with xmodmap -e "command", but for bigger changes it is easier to dump the tables to a file which can then be edited and reinserted.

xmodmap -pke will print the tables in a format which can be fed back in, so redirect the output or pipe it into a text editor and make any changes you desire. The syntax is similar to that of loadkeys, but the grammar is different - man xmodmap or xmodmap -grammar will display it. Keycodes are also different to those generated by showkey; the xev program will do that for you. Look for "keycode xx". The keysyms available also differ from those of loadkeys, and can be found at List of keysyms. Once the map is finished, send it to xmodmap with

xmodmap filename

Warning. setxkbmap not only alters the alphanumeric keys to the values given in the map; it also resets all other keys to the startup default. If you have assigned values to the Windows keys or the multimedia keys, these settings may be lost.

Enabling Keyboard Multimedia Keys

Exactly how this is done depends upon which desktop manager you are using, a quick guide below for the common environments.

General Instructions

The Gnome and KDE desktops enable many media keys to be set using the "shortcuts" menu, as described below. Otherwise, if your keyboard isn't supported you will need to make a custom keymap (xmodmap) that should be loaded each time X starts. Here's how you do it.

  • Open a terminal while you are in X and start xev (a window will pop up with the title "Event tester")

Place you cursor on the window
Now when you type letters on your keyboard information about those key presses will be displayed in the terminal. Here's an example of what it prints out for me when I hit Page Up:

KeyPress event, serial 28, synthetic NO, window 0x3400001,
    root 0x38, subw 0x0, time 636222625, (124,70), root:(1225,548),
    state 0x10, keycode 99 (keysym 0xff55, Prior), same_screen YES,
    XLookupString gives 0 bytes:  ""

As you can see the key next has the keycode 99 and the symbol Prior. What you should do now is press your multimedia keys. Note that if your keyboard is unconfigured it will say "No Symbol". Now look what keycode it says for each multimedia key you press. As an example my "Labtec Internet keyboard" has a key labeld with a stop symbol and the output for it is:

KeyRelease event, serial 28, synthetic NO, window 0x3400001,
    root 0x38, subw 0x0, time 636666629, (158,102), root:(832,903),
    state 0x10, keycode 164 (keysym 0x0, NoSymbol), same_screen YES,
    XLookupString gives 0 bytes:  ""

So I write on a piece of paper
Stop = 164
Do this for each of your keyboards multimedia keys. When you are done you can close the Event Tester.

  • Select names symbols (names) for your keys

You have to chose symbols for your keys from this list.

  • Write xmodmap -pke > xmodmap.conf

This will output your current keymap to a file called xmodmap.conf so that we can add symbols to those keycodes. Open that file with your favorite text editor. Now find the keycodes of your mulimedia keys and write the appropriate symbol after it. So for my stop key I chose the symbol XF86AudioStop wich will look like this
keycode 164 = XF86AudioStop
When you are done remember to SAVE THE FILE!

  • To have your new keymap loaded every time X starts (you will need to restart X for that) do
    echo 'xmodmap xmodmap.conf' >> ~/.xinitrc
  • To manually load your xmodmap (takes effect immediately) do
    xmodmap xmodmap.conf

Users can send their keyboard's keycode symbol mappings to this list. Take a look if you're looking for ready-to-use map for your keymap.


Start the KDE Control Center (kcontrol), chose Regional & Accessibility => Keyboard Shortcuts. Here you can:

  • define which Shortcut starts which application
  • define which shortcut does which action (Window to Desktop 1 etc.)
  • assign keys to be modifiers


For the current version of Gnome, enter the menu for System - Preferences - Keyboard shortcuts. This enables you to set the sound controls and launcher keys for the e-mail and web-browser programs.

An older solution is acme.

  1. Download and install acme (source available here from ftp site]. this software enables gnome to interpret keyboard multimedia keys.
  2. After rebooting, open the Gnome Control Centre.
  3. Open the "Multimedia Keys" icon.
  4. The dialog you are presented with allows you to configure which key on your keyboard does what. Go through the list selecting the relevant key on your keyboard.
  5. Now enable the Multimedia keys by clicking "Launch the Multimedia Key Daemon"

To enable the keys on every boot acme must be added to your profiles startup programs. This can be done by returning to control center => Advanced => Sessions, select the Startup Programs tab, click add and enter "acme".

Enabling a compose key

If you press a ` and an e, you get an è. In this case the key ` is called a dead key because it is at first stroke dead. A bit more tricky is the compose key. Press the compose key and then type two keys, and both keys will be combined. E.g. type the compose key, then the comma, then c and the outcome is ç.

Desktops like Gnome and KDE enable you to nominate a key, such as Menu, Right Windows, or Right Control, using the keyboard configuration option.

Alternatively, you can use xmodmap:

xmodmap -e "keycode 134 = Multi_key"

This makes your compose key is 134, the right Windows-key on your keyboard.

A third option is to modify the "InputDevice" section of /etc/X11/xorg.conf, by adding a line like

Option       "XkbOptions"  "compose:rwin"

to set the right Windows-key as compose-key and Restart your X Windowing System. This is a rather old-fashioned solution, for many distributions no longer use this file.

Disabling Numlock at startup

Switch to root (via command "su"), go to /etc/X11/xinit.d. There has to be a file "numlock". If not in that particular directory, then find the file "numlock", it has to be somewhere in /etc/X11. In that file comment out everything with a "#" key, so that nothing happens to numlock status at startup, for example:

#if [ -f /var/lock/subsys/numlock -a  -x /usr/bin/enable_X11_numlock ]; then
#    /usr/bin/enable_X11_numlock

Altering or Creating Keyboard Maps

If none of the available keyboard maps in this wiki is entirely suitable, it is easy to alter or create one.

Illuminated keyboards

Illuminated keyboards typically have the illumination linked to one of the LEDs. For example, the Media-Tech M-GLO MT1217 uses the Scroll Lock LED for this. To enable illumination, you can use the command:

xset led 3

and to turn it off:

xset -led 3

See also