Altering or Creating Keyboard Maps

From LQWiki
Jump to: navigation, search

If none of the keyboard layouts provided for X-window does exactly what you want, it is easy to modify one of them, or to create and install a new one. It is obviously a good idea to keep a copy of the old one before altering it! In the Fedora distribution, the keyboard files are kept in /usr/share/X11/xkb/symbols/; in Ubuntu, they are in /etc/X11/xkb/symbols/. If neither applies, search for symbols. The file "latin" gives a basic QWERTY keyboard; "gb" defines those keys that differ in the British one and refers the computer to the other listing with the command

include “latin”

In addition to the map for the alphanumeric keys, which varies according to the language selected, Linux also uses a map for the other keys, which is chosen automatically when your keyboard is detected. For most users, this will be one of the variants in the file pc. The language map takes precedence, so alterations to keys like Delete or Caps Lock can be incorporated into it to save having to alter the hardware map as well.

Map Structure

To define a key one needs to give its identifier and up to four uses: alone, with <Shift>, with <AltGr>, and with both modifiers:

key <AE04> { [ 4, dollar, EuroSign, onequarter ] };

The identifier <AE04> means that the key is the fourth (04) in the fifth row up (E) of the alphanumeric area (A). Some distributions identify the key by its scan code: in this case 13. The scan codes can be found with the utility xev if necessary, although the existing keyboard files should enable them to be identified. Identifiers for other keys, like <LWIN> or <PAUS> can be discovered by examining the file pc.

The uses of the key can be identified by giving the character (∅), a recognised name (emptyset), or a code (U2205). Giving the actual character may occasionally cause erratic behaviour, and is best confined to those characters on the keyboard or in Latin-1. Functional keys have actions, like Caps_Lock or ISO_Next_Group, which can be discovered from the file pc.

If the AltGr key is to be used, the definition must include

include "level3(ralt_switch)"

Installing a new Key Map

All that is needed is to place the file in the …/xkb//symbols directory and make entries in two files.

In …/xkb/rules/base.xml make a new entry in the layout list, along the lines of the following:

    <description>United Kingdom</description>

<name> is the name of the file in the symbols directory; <shortDescription> is the identifier used in the panel keyboard indicator; <description> is the identifier used by the menu when you are installing keyboards.

In …/xkb/rules/base.lst add to the "! layout" block the file name and description of your keyboard. e.g.

gb	United Kingdom
If you get a warning on rebooting that there is a bug in your keyboard server, there isn't; it's only an error in your keyboard map.

Using Multiple Keyboard Layouts

If you need different characters on different occasions, you will need more than one keyboard layout. I have phonetic symbols on my British keyboard, and a special keyboard for astrological symbols: AltGr+g produces "ɣ" with one and "♂" with the other. Similarly, with both English and Russian layouts available, it is easy to produce a document in one language with commentary in the other.


In the Gnome desktop, the menu option System - Preferences - Hardware - Keyboard has a tab "Layouts" where you can select up to three extra virtual keyboards to use alongside the default one.

To show which keyboard is in use, you can add the Keyboard Indicator to the panel. Another useful feature, available in System - Preferences - Hardware - Keyboard, is to use the Scroll-Lock light to show when a layout other than the default is in use.

In System - Preferences - Hardware - Keyboard, the tab "Layout Options" enables you to set the "Group Shift/Lock Behaviour": to choose a key which will change the key map in use. I use the option ‘Menu key changes group’ so that I have three related keys in a row: AltGr, Compose, and Group Shift. You can also change keyboards by left-clicking on the panel indicator, or view the layout by right-clicking.

If you have created a keyboard with a distinctive name, you cannot install it using System - Preferences, because that only allows you to select a recognised language. Instead, you must use System Tools - Configuration Editor. Run this and select desktop - gnome -peripherals - keyboard - general - kbd. The first line can then be edited to give a result like

layouts [gb,astro]


Start kcontrol, chose Regional & Accessibility -> Keyboard Layouts -> Enable Keyboard Layouts. Add the layouts you want. A country flag icon appears in your systray down right. Change your keyboard layout by clicking on the icon.

See also