- Karol Stasiak’s Blog
- Coding from an elevator.
- Creating custom keyboard layouts for Linux
- Ubuntu Documentation
- Introduction
- Background
- Quickref
- Details
- Editing an existing layout
- symbols/es
- Creating a new layout from scratch
- symbols
- symbols/ht
- How to load changes
- Notes
- Unicode and alt codes
- Further contributions
- External Links
- References
Karol Stasiak’s Blog
Coding from an elevator.
Creating custom keyboard layouts for Linux
I think almost every one of you has some beef with your keyboard layout. There are characters you’re never going to use, there are characters you’d love to use but they’re missing (and you don’t like/can’t use the compose key for them), and so on.
For example, this is the default Polish layout in Linux:
A keen eye can notice several things that could be improved:
many characters can be input in multiple ways: [ (as [ or as AltGr-9 ), ł (as AltGr-w or AltGr-l ), & (as Shift-7 or AltGr-Shift-k ), @ (as Shift-2 or AltGr-q ),
the layout has some unassigned combinations (for example AltGr-Shift-4 ),
some important characters are missing: € (the euro sign) and „ (Polish opening quote),
since I can use the compose key, I don’t need all those arbitrarily assigned dead keys on the right.
Of course your keyboard layout may have different problems.
Disclaimer: The following was done on Ubuntu 12.04 with Unity DE, I don’t guarantee it will work on other distros. In particular, the file paths can differ.
How to create a new one, you ask? That’s actually pretty simple.
layout definitions are in the /usr/share/X11/xkb/symbols directory,
layout metadata are in the /usr/share/X11/xkb/rules/evdev.xml file.
Let’s first define our layout. The /usr/share/X11/xkb/symbols contains files, each corresponding to a language. Some layouts can belong to several languages, but they’re defined in only one of the files. If your layout is supposed to be listed under one language in the keyboard setting anyway, you’ll have to add it to the correct file. Since my keyboard layout was to be mostly an improvement on top of the Polish language layout, I decided to add it to /usr/share/X11/xkb/symbols/pl .
The format of this file is pretty simple. I’m going to analyse the default Polish layout line by line.
default means that this is the default layout in this file (other layouts don’t have this tag).
«basic» is the indentifier of this layout within the file.
Since most layouts are similar, the include directive simply inserts all key definitions from one layout to another. This one includes the (default) «basic» layout from the /usr/share/X11/xkb/symbols/latin file.
No clue what it does. It’s recommended to set it to a full English name for the layout.
Those are the definitions that make the Polish layout different from the default Latin one.Each line contains:
the key keyword,
a symbol defining the physical key,
a list of characters associated with that key, from left to right: the default one, with Shift key, with AltGr key, with both Shift and AltGr keys.
The names of the physical keys are as follows (I’m referring to keys by their American/Polish usage):
for the grave accent/tilde key
for the backslash/bar key
Note: this refers to their physical layout, so I could also say that AB is the row above the spacebar, AC is the row with caps lock key, AD is the row with tab key, and AE is the row with digits, and the numbering goes from left to right. For example, is labelled [ on the American keyboard and Ü on the German one; is Z on the American keyboard, W on the French keyboard, and Y on the German keyboard. is always in the top left, just under the Esc key, and is in either AC , AD or AE row (depending on the keyboard).
Also, for some reason trying to override a key with a shorter definition doesn’t work.
As for the character codes, you can look around the files to learn them, or just use Unicode codepoints in hexadecimal, for example U0041 and A are synonymous.
This defines the behaviour of the decimal point key on the numpad. (More accurately, imports such behaviour override from /usr/share/X11/xkb/symbols/kpdl .)
And finally, this defines how the 3rd level (the one with accented characters, the one I referred to as “AltGr”) is accessed. In this case, it’s with the right Alt key.
So I created my own layout by copying the existing one and modifying it:
«custom3» is the identifier of my new layout (why 3, I’ll explain later). My new layout is not defined as the default one.
No clue why I did this, but it looks consistent with the rest of the file, so I guess it’s correct.
As you can see, I added an euro sign to Shift-AltGr-4 , opening quotes to the J key, and so on.
Okay, my layout is ready. Time to hook it in.
Now we’re editing the /usr/share/X11/xkb/rules/evdev.xml file. The interesting fragment looks like this:
What does it mean? configItem/name is the name of the file in /usr/share/X11/xkb/symbols that contains layout definitions. languageList is a list of languages which that file supports. variantList is a list of alternative layouts that are defined in that file and their user-friendly names (which will be translated if there’s translation for them).
Since I didn’t want to add the translation for the name of my layout, I simply added an entry with the name hardcoded in Polish:
Also, it’s time to explain why it’s custom3 , not custom . Since those files are cached in memory by the DE, if you change the layout in the /usr/share/X11/xkb/symbols/* file, those changes won’t be reloaded. Since I didn’t want to restart everything just to test the layout, I solved that problem by adding 2 (and later 3) to the identifier.
And this is the result:
Still not perfect, but way better.
Posted by Karol Stasiak 2013-11-18 keyboard, Linux, Unicode
Источник
Ubuntu Documentation
Needs Updating
This article needs updating to include the latest versions of Ubuntu. More info.
Introduction
This tutorial will introduce you to the basics of modifying and creating custom keyboard layouts for use with the system-standard gnome-keyboard-properties application (usually accessed through System->Preferences->Keyboads).
Background
Quickref
Details
The basic keyboard mapping package is XKB. In current Ubuntus, the default directory is not /etc/X11/xkb but rather /usr/share/X11/xkb. Layouts are kept in the symbols directory therein: they are generally named by a two letter country code associated with the language for which the layout is designed.
Editing a layout is as simple as locating the correct file in the symbols directory and editing the file. Adding a new layout requires that you also add the file to rules/evdev.xml. If you use other X11 wms you may want to give your layout to the rest of the xorg system. You can do this by adding the file to the symbols.dir, and adding a line for the file in rules/evdev.lst under the !layouts section. These steps are currently beyond the scope of this document.
The output from layout files can be any Unicode character (see this Wikipedia entry), and are entered with the U and without the leading + sign, as for example U0282. Numbers and the standard alphabet may be entered directly. Dead keys, signs, and a large repertoire or characters, such as the @ «at» sign, have a descriptive name that can be entered in place of finding the Unicode character directly — these are defined in /usr/include/X11/keysymdef.h. Ignoring «XK_» part after each definition, the remainder of the word may be used in your layouts (for the sterling symbol, it says #define XK_sterling . , so you would use sterling in your layout).
See the External Links section for a general overview of XKB and more.
Editing an existing layout
For example, suppose you have a Spanish language keyboard and want to add a dead key for the tilde accent for chatting with your Brazilian friends. You might then opt to place the tilde accent on the ñ key, because it has that symbol printed right on the key (albeit over the «n»).
To do this, first you need to find the keyboard layout file you are using — in this case, it would typically be the /usr/share/X11/xkb/symbols/es file.
symbols/es
Each line that starts with key gives a definition for that key. The word after key specifies which key, such that the second letter is A for the bottom row, B for the second row, etc., and the number is how far to the right that key is in that row (starting at zero). So mostly everyone’s Q key is AD01 (with AD00 being the tab key). The function keys follow a different naming formulation, FK##, and other keys such as the key to the left of the «1» key, may be called TLDE. These are specified in keycodes/evdev by default.
After that there are blocks of generally 4 or less assignments to each key. Each column specifies a different modifier:
unmodified
shift+altgr
* altgr is the right alt key
In the «basic» section, you will be looking for the line containing the ñ key, which should look something like this:
Say you want to replace where it says «asciitilde» to be the dead key for the tilde accent (the same symbol, but as a dead key). Looking in keysymdef.h, you can see that it has the predictable (if somewhat unfortunate) name dead_tilde — so you substitute it in like so:
Then restart X windows and the change should be applied.
If the change doesn’t seem to have any effect, it’s possible that you might have edited the basic settings in the symbols/es file. There may be a section for your specific keyboard layout, e.g like this:
Applying the change to the proper section should then make the customization work.
Note that you may also have to clear the pre-compiled keymaps before your modifications work:
Creating a new layout from scratch
For this exercise we’re going to create a layout for Haitian Creyole. Kreyòl Asisyen, as it is called, is not supported by default, although oddly Latin American layout for Haitian is supported. I can’t find any references for a proper layout so I’m going to be using my personal understanding of haitian to guide the design. — Even though my understanding is certainly basic.
symbols
To create a new layout from scratch we have to create our own symbols file. The two letter country code for Haiti is «ht», so following in the footsteps of all the other layouts that people ahve made in the symbols directory, let’s use that name:
Okay, so that’s cheating. This isn’t in school, so get over it. There’s a whole section, about half of the entire layout, for dvorak typing. I don’t know enough about character frequencies in kreyòl, so I’m just gonna cut that. The «cat» and «ast» sections are for Catalán and Asturian, related languages to Spanish, so I cut them as well. I also don’t have an OLPC to work with, so I cut that section.
In what remains I need to rename Spain to Haiti everywhere. I need to replace the country code es with ht. And then, the hard work, I need to actually come up with a good key mapping for the haitian language. This is what the kreyòl alphabet looks like at Wikipedia. You can see there’s a lot (a whole lot) of dipthongs and trithongs, they won’t have a one-to-one mapping in unicode (I don’t think so), so we’ll just provide each glyph for those letters. There’s no non-latin characters or anything special going on, but we want dead keys for the standard accents: grave, acute, diaresis, and caret. They tend to use quotes much as the italians or french do, with Guilemets for antural quotes and double qoutation marks for short, more formal citations or «quote-words». And they use hyphens and apostrophe’s in their text much as in português or english.
symbols/ht
example symbols/ht file
The top name tag there is the filename under the symbols directory. The country list is a list of which countries should house this keyboard layout — short list in our case. Likewise with the language list we only needed to put «hat» as it’s just for haitian creyole. There were segments we defined for sun dead keys and we have to find each xkb_symbols » » and create a sectio like above for it, if we want to see it in our list of variants for that laguage or that country when we browse to it in the keyboard preferences layout pane.
Close any such preferences and open it (go to System->Preferences->Keyboards), on the layouts tab when we add a layout we should now see our new haitian keyboard defitions under Country: Haiti, and as well under Languages: Haitian (Haitian Creyole).
How to load changes
To load changes to the layouts menu simply close out of gnome-keyboard-preferences and reload, relaunching the windows manager should not be necessary.
There is a change in Ubuntu version (13.10) and later versions which causes the keyboard settings cache to not refresh after files in «/usr/share/X11/xkb/symbols» directory are modified. It looks like changes just don’t get applied. To force the cache refreshing a one should delete *.xkm files from «/var/lib/xkb», or enter into the console:
Notes
Unicode and alt codes
(The Combining characters code blocks are particularly useful references for the following:)
From stylishpants on the forum:
GNOME provides a ‘Character Map’ utility (Applications/Accessories/Character Map) which displays characters ordered by Unicode block or by writing system, and allows searching by character name or extended description. Where the character’s code point is known, it can be entered in accordance with ISO 14755: hold down Ctrl and Shift and enter the hexadecimal Unicode value, preceded by the letter U if using GNOME 2.15 or later. The input code is an UTF-32 value.
For example, type Ctrl+Shift+U30AD to type a Katakana letter ‘キ’ Ki. This means, press and hold Ctrl and Shift, then press and release each of the keys u30ad in turn, then release Ctrl and shift.
In Windows you enter the decimal value, but here you’re entering the hexadecimal value (which is good, because it’s usually shorter to type.) You can translate between decimal and hexadecimal with the ‘calculator’ utility (use the ‘scientific’ setting).
This reveals the following:
British pound
decimal 0163
Copyright symbol
decimal 0169
Alternatively you can find the hex value just by clicking on the symbol in Character map. (You probably want to choose the ‘Common’ script from the left-side menu.)
Note that the article also provides special methods to input characters at the Linux terminal and in the Vim editor (as well as other operating systems.)
Further contributions
Further contributions: it would be nice to at least add a section on how to set up media keys as well. Two other sections that would be appreciated if someone wants to write it up would be how to set up multi-second-key dead keys (so you can emulate windows alt+160, etc) and how to spawn more than one glyph from a keypress (so you can get a true ch glyph for, for example, the kreyòl example above).
External Links
keysymdef.h — file:/// url to the keysym definitions on your computer.
XKB configuration files — solid non-Ubuntu-specific tutorial
X keyboard extension — Wiipedia overview of the XKB system
Unicode and alt-code input — ubuntu forum thread on unicode input and equivalences to windows style alt codes.
References
XKB pages — X.org foundations’ documentation.
Custom keyboard layout definitions (последним исправлял пользователь waldyrious 2021-07-14 14:02:25)
The material on this wiki is available under a free license, see Copyright / License for details
You can contribute to this wiki, see Wiki Guide for details
Источник