This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Enabling custom keyboard definition files

Tags: None
(comma "," separated)
Registered Member
I have a modified keyboard definition file that I have used for years with KDE3 on Kubuntu 8.04. I recently moved to KDE4 on Kubuntu 10.04, which appears to have the identical structure underneath.

According to my notes, I put the keyboard definition file in /usr/share/X11/xkb/symbols

I then hand edit in KDE4 with a root editor the /usr/share/X11/xkb/rules/base.lst to include my custom keyboard file. Excerpt below:

Code: Select all
  benqx           BenQ X-Touch
  hhk             Happy Hacking Keyboard
  classmate       Classmate PC
  olpc            OLPC

! layout
  us              USA
  us_nuer     Nuer
  ad              Andorra
  af              Afghanistan
  ara             Arabic
  al              Albania

The custom file is the one called us_nuer, and that is also its filename in the filesystem. That file is owned by root, and belongs to the group 'root' and has the same file permissions as all the others in that directory. This file was copied from my KDE3 machine.

Once that is done, I reboot. Then, in KDE3 (or KDE4) I go to Control Center | Regional and Language | Keyboard Layout. From here I check the 'Enable Keyboard Layouts' checkbox.

At this point, in KDE3, my custom keyboard layout appears in the list in the lower left pane. I select it, move it to the right pane, and select the variant I want. Save, and I'm done.

In KDE4.4.2 on Kubuntu 10.04, my keyboard file does NOT show up in the lower left pane, and nothing I do seems to fix this.

What's up? Is the base.lst file being read from somewhere else now? xfree86.lst is a symlink to the base.lst file, just in case someone mentions that.


Registered Member
Creating custom keyboard layouts for X11 using XKB: 8)

In most modern desktop environments there is a small applet that allows users to quickly switch between keyboard layouts when they need to type text in more than one language. However, there are situations where this solution is not quite satisfactory. Some writing jobs (that of a translator, for example) require writing text in which words from several languages are intermixed, which would make frequent switches between keyboard layouts necessary. This is a big inconvenience, even when keyboard shortcuts are used to switch between layouts. Another problem is that the logical keyboard layouts used may sometimes not fit the physical keyboard layout very well, resulting in a confusing setup.

One solution is to use layouts with so called dead keys. This is a way of generating accented characters by first pressing a key which corresponds to the accent (the dead key) and then a letter key (e.g. pressing ~ and then n generates ñ). This allows you to generate many accented characters using just a few keys, since a dead key need not be a single keystroke but can also be a combination of keys. For example, the backtick key is often used as a dead key and generates four different accents depending on whether it's pressed alone or with combinations of Shift and AltGr (the right Alt key). This is a clever trick, but writing using dead keys is usually less convenient than using a simple combination like AltGr+letter. Dead keys are a good solution for characters that are seldom used, but for characters that need to be typed often, we need a more convenient technique.

kxkb - the KDE keyboard layout switcher - showing a custom keyboard layout The X Window System used on most Unix-like systems today uses X Keyboard Extension (XKB) for translating keystrokes into character codes. Thanks to XKB's flexibility, one can easily create custom keyboard layouts that can help solve the problems mentioned in the preceding paragraphs. This tutorial describes how to create a custom keyboard layout combining diacritic characters from several standard layouts, thus eliminating the need to switch keyboard layouts while writing. On the right is an image showing the final result: a custom keyboard layout called de_pl, listed together with standard layouts in the KDE Control Center.

Throughout this document I will use a keyboard layout combining Polish and German diacritic characters as an example. Standard layouts for Polish and German are quite different, and switching between them while writing is very inconvenient. The Polish layout is essentially the same as a standard US keyboard, the only difference being Polish diacritic characters (ĄĆĘŁŃÓŚŻŹ) which are generated using AltGr and the corresponding nonaccented letter (AltGr+X is used for Ź). The German layout is radically different, since it is a QWERTZ layout. The differing position of Y and Z alone is enough to make the writer's life really hard. But that's not all: separate keys are reserved for German umlauts ÄÖÜ and ß, causing almost all punctuation and special characters like &, +, /, <, >, etc., to have a different position than in the Polish layout. Writing any text where characters from both languages appear and having to switch between the two layouts is very inconvenient.

Our goal is to create a keyboard layout combining accented letters from both languages that fits well to the physical layout of a German keyboard. Thus we want it to essentially be a German layout (preserving the position of punctuation marks and umlauts as well as of Y and Z) but we want to bind additional AltGr+letter combinations to Polish accented characters.

XKB layouts are defined by text files living in /etc/X11/xkb/. On some systems they may be placed in /usr/X11R6/lib/X11/xkb/ (on my machine, the later is a symlink to the former directory). Newer versions of Fedora and possibly other RedHat-based distributions use the location /usr/share/X11/xkb. There are several subdirectories in that folder, but for the simple task of creating a new layout we are interested only in the symbols subdirectory. Root permissions are necessary for most tasks described below, as all configuration files in this directory are owned by root. Note that some of the files we need to edit are read-only even for the superuser, so it may be necessary to change their access permissions before editing (most text editors allow saving read-only files after confirming the operation, though).

Files to describe the basic keyboard layouts are given the names of their language name abbreviations (e.g. de for German). Names such as ge_la are used for keyboard variants (la (Latin) variant of the ge (Georgian) keyboard). Thus, in order to follow this convention, we use de_pl for our new keyboard layout instead of e.g. depl. Using this naming scheme is not mandatory — the layout would also work under another name — but recommended. Some applications, such as the KDE Control Center, also rely upon this naming scheme in order to assign icons (national flags) to keyboard layouts.

We go to the /etc/X11/xkb/symbols/pc/ directory (if a non-PC keyboard model is to be used, all actions should be performed in /etc/X11/xkb/symbols/ instead) and copy the file de to de_pl in the same directory.

Let's make some modifications to our new layout. Open de_pl in a text editor and find the section that assigns character codes to keys. It should look similar to this (only a small part is quoted here for brevity):

key <AE02> { [ 2, quotedbl, twosuperior, oneeighth ] };
key <AE03> { [ 3, section, threesuperior, sterling ] };
key <AE04> { [ 4, dollar, onequarter, currency ] };
key <AE11> { [ ssharp, question, backslash, questiondown ] };
key <AE12> { [dead_acute, dead_grave, dead_cedilla, dead_ogonek ] };
key <AD03> { [ e, E, EuroSign, EuroSign ] };
key <AD06> { [ z, Z, leftarrow, yen ] };
key <AD11> { [udiaeresis, Udiaeresis, dead_diaeresis, dead_abovering ] };
key <AD12> { [ plus, asterisk, dead_tilde, dead_macron ] };

The meaning of these lines is obvious — they assign characters to key positions on the keyboard. For example <AE03> means the third alphanumeric key in the fifth row (the letters A, B, etc., denote key row numbers counted from the bottom of the keyboard). Special keys have their own names — e.g. <ESC>, <TAB>, <RTRN> etc. Character names are usually self-explanatory and in most cases identical or very similar to Unicode names. A complete list of all available character names can be found in file /usr/X11/include/X11/keysymdef.h in the form of constant definitions for C programs.

Each line of the form #define XK_xyz corresponds to the character name xyz that can be used in keyboard layout definition files. However, usually it is easiest to just copy some lines directly from other keyboard layouts (in this case, from file pl, which defines the Polish layout). The four character names assigned to each key are characters generated when the key is pressed alone, with the Shift key pressed, with the Multi_key key (which is defined to be AltGr in this layout) pressed, and with both Shift and Multi_key pressed, respectively.

For example, we can use AltGr+Z to generate the character Ż (lower/uppercase depending on Shift state) instead of a left-pointing arrow and the yen symbol by changing the corresponding line to:

key <AD06> { [ z, Z, zabovedot, Zabovedot ] };

In a similar manner, we can add or modify the corresponding characters' descriptions to get all the other Polish characters. (Ł is bound to AltGr+L in the German layout already so there's no need to add it.):

key <AD03> { [ e, E, eogonek, Eogonek ] };
key <AD09> { [ o, O, oacute, Oacute ] };
key <AC01> { [ a, A, aogonek, Aogonek ] };
key <AC02> { [ s, S, sacute, Sacute ] };
key <AB02> { [ x, X, zacute, Zacute ] };
key <AB03> { [ c, C, cacute, Cacute ] };
key <AB06> { [ n, N, nacute, Nacute ] };

As the last touch, we bind the euro sign to AltGr+5, since the original AltGr+E binding is used for Ę now:

key <AE05> { [ 5, percent, EuroSign, EuroSign ] };

If you happen to once in a while use characters from many different languages (which you don't use often enough to bother creating separate keybindings for), you may find using the Multi Key (also called Compose Key) very useful. This is a special key, on PC keyboards usually assigned to the right Windows Key, which allows typing accented characters by entering a character corresponding to some accent and then an unaccented letter. For example, by typing Compose, then the comma and then C, you generate the C-cedilla (Ç). Note that you need to press the keys in sequence — Compose doesn't work like Alt or Shift which you press together with another key; you have to release Compose before typing in the next character. Similarily to creating C-cedilla, you can generate letters such as à, á, â and ä by composing a with the backtick, the apostrophe, the ^ character and the quotation mark ("), correspondingly. By composing the semicolon (;) with e or a you can get the Polish "ogonek" as seen in the letters Ę and Ą. Using Compose with the slash (/) you can type Ø, Ł or Ŧ. The tilde (~) can be composed with N to generate the Spanish Ñ while Compose followed by two question marks (?) gives the inverted question mark ¿. Many other ligatures and special characters, including punctuation, can be generated in a similar manner. Composing the letter O with R or C gives the registered trademark and copyright signs ® and ©. Composing S with another S gives Scharfes-S (ß) and composing A with E results in Æ. Composing two less-than or two greater-than signs produces opening or closing guillemets « and » while composing the percent sign with the letter O results in the permille sign ‰. Using this technique, the majority of all accented and special letters used in European languages can be typed. See this file for a complete list of compositions available in X-server (look for lines starting with <Multi_key>). Other X servers may support slightly different combinations.

Some keyboard layouts make the right Windows Key function as Compose Key by default while others don't, so just to make sure you can use this functionality, add the following line to your keyboard layout definition:

key <RWIN> { [ Multi_key ] };

This makes our new keyboard layout description complete.

The last step is letting X know that a new layout was added. The file /etc/X11/xkb/rules/xfree86.lst contains a list of available layouts together with their descriptions. Adding the below line:

de_pl German (with Polish characters)

makes our newly created layout usable in X. In order to make the keyboard layout available in GNOME, you should also modify /etc/X11/xkb/rules/xfree86.xml accordingly (this is an XML file and the format is mostly self-explanatory). If you want to make the new layout the default (which is probably the case), edit /etc/X11/XF86Config (sometimes /etc/X11/XF86Config-4 is used) and modify the InputDevice section for your keyboard to contain the line:

Option "XkbLayout" "de_pl"

After modifying these files, restart the X server by pressing Ctrl+Alt+Backspace. The new layout should now be loaded if you made it the default in XF86Config. If not, you can select it from the list of layouts in your favorite arabic keyboard layout switching applet.
Registered Member

I would like to thank @taoufiktito for this detailed instruction, it was exactly what I was looking for!

FYI, I was constantly switching between German/English/Russian/Polish, it was so annoying! (Entering passwords with special characters on different keys, typing "Z" vs. "Y", and typing in general...).
Now I have configured a custom layout "English (+ Polish/German)" with Polish characters ą, ę, ż, ź, ć, ó, ł, ń, etc. and German characters ä, ö, ü and ß.
I use - as you suggested - a dead key for the diaeresis (= the umlaut dots = the thing with the two dots above the a, o, u) with ALTGR + U, and ALTGR + 4 for the ß.
Russian is still a separate layout, of course (but you can also type Belarusian/Ukrainian keys with ALTGR).

I changed the following under "/usr/share/X11/xkb/symbols/pl" (the path may vary depending on your Linux distro):
Code: Select all
key <AD07>  {[ u, U, dead_diaeresis, U2194 ]}; // dead key diaeresis -> German character ä, ö, ü with ALTGR + U
key <AD07>  {[ 4, dollar, ssharp, onequarter ]}; // sharp s -> German character ß with ALTGR + 4

Again, thanks a lot, it's clearly more efficient to type this way, since you don't have to worry about a layout. :)


Who is online

Registered users: Bing [Bot], Google [Bot], kesang, Yahoo [Bot]