How to set up a custom keyboard layout?
I wish I could design a keyboard layout of my own (specifying symbols for each the key to be entered in case it is pressed alone, with Shift or with AltGr ) and set it up among all the other layouts in my XUbuntu 11.04 system. How to do this?
1 Answer 1
I use a local keyboard with US and Thai layout.
The us layout is ok for me, but I like to have some additional german characters like äöü ÄÖÜ This can easy be implemented with xmodmap , but if you switch to the second layout, you will observe some strange results.
I found a good solution: create and use your custom keyboard!
- /usr/share/X11/xkb/symbols/ choose your nearest preference, in my case us and save it with a new name: us-de
- delete anything, which you don’t need, and change, what ever you like. My file for us-de :
default xkb_symbols "basic" < name[Group1]= "ASCII with german"; // Alphanumeric section key < [ grave, asciitilde ] >; key < [ 1, exclam ] >; key < [ 2, at, twosuperior ] >; key < [ 3, numbersign, threesuperior ] >; key < [ 4, dollar, onequarter ] >; key < [ 5, percent, onehalf ] >; key < [ 6, asciicircum, threequarters ] >; key < [ 7, ampersand ] >; key < [ 8, asterisk, oneeighth ] >; key < [ 9, parenleft ] >; key < [ 0, parenright, degree ] >; key < [ minus, underscore ] >; key < [ equal, plus ] >; key < [ q, Q ] >; key < [ w, W ] >; key < [ e, E, EuroSign, cent ] >; key < [ r, R ] >; key < [ t, T ] >; key < [ y, Y ] >; key < [ u, U, udiaeresis, Udiaeresis ] >; key < [ i, I ] >; key < [ o, O, odiaeresis, Odiaeresis ] >; key < [ p, P ] >; key < [ bracketleft, braceleft ] >; key < [ bracketright, braceright ] >; key < [ a, A, adiaeresis, Adiaeresis ] >; key < [ s, S ] >; key < [ d, D ] >; key < [ f, F ] >; key < [ g, G ] >; key < [ h, H ] >; key < [ j, J ] >; key < [ k, K ] >; key < [ l, L ] >; key < [ semicolon, colon ] >; key < [ apostrophe, quotedbl ] >; key < [ z, Z ] >; key < [ x, X ] >; key < [ c, C ] >; key < [ v, V ] >; key < [ b, B ] >; key < [ n, N ] >; key < [ m, M, mu ] >; key < [ comma, less ] >; key < [ period, greater ] >; key < [ slash, question ] >; key < [ backslash, bar ] >; key < [ VoidSymbol ] >; // End alphanumeric section include "level3(ralt_switch)" >;
us-de US-DE ASCII with german eng
- a) us-de is the filename of the new keyboard layout in X11/xkb/symbols
- b) US-DE this will appear eg. in the indicator applet
- c) ASCII with german must coincide with the text at the begin of your file:
name[Group1]= «ASCII with german» ; This text will also appear as comment under «Layouts» - d) if you choose «eng» your layout will be shown in System->Preferences->Keyboard->tab Layouts->Add->By Language->English under «Variants»
Work In Progress
This guide is an early work in progress, some information is incomplete and may change in the future.
Creating new keyboard layouts
This tutorial will take you through the steps required to create a new keyboard layout for the Ubuntu keyboard. To illustrate this we’ll create a brand new layout for the Welsh language. This will include defining the layout itself, setting the dictionary used for spell checking and training the prediction module on our new language.
Welsh has the language code «cy», for your own layout you’ll need to replace any instances of «cy» with your own layout’s language code.
Checking out the keyboard
All keyboard layouts live inside the ubuntu-keyboard project on launchpad, so we’ll create a new branch of ubuntu-keyboard and add our layout to this.To create a new branch of the keyboard simply run:
bzr branch lp:ubuntu-keyboard add-welsh-layout
Where «add-welsh-layout» is the name of the directory you wish to create the branch in.
Template
To simplify the creation of new layouts you can use the layout generation tool — The template generation tool isn’t complete yet, instead we recommend looking at a the changes made in a recent branch which adds the Catalan layout: https://code.launchpad.net/~dpm/ubuntu-keyboard/add-catalan/+merge/238368
Layout Files
There are typically four different layouts specified for each language, the default layout (in our case «Keyboard_cy.qml»), the URL layout («Keyboard_cy_url.qml»), the search URL layout («Keyboard_cy_url_search.qml») and the email layout («Keyboard_cy_email.qml»).
The keyboard layouts are implemented as QML files, which make use of a number of custom components common to all layouts:
- BackspaceKey — Triggers the deletion of characters, this should typically be placed at the end of the third row.
- CharKey — This is used for all normal character keys. The character it inputs can be set via the «label» property, the character that should be input when the keyboard is in a shifted state can be set via the «shifted» property. CharKeys can also provide access to an extended set of characters that the user accesses by holding down the key for a short time, these are set via the «extended» and «extendedShifted» properties. The first row of characters should typically have the numbers 0-9 as the first entries for the extended and extendedShifted on the top row of the keyboard.
- LanguageKey — Provides access to the language switcher menu, this key is only visible if the user has more than one keyboard layout enabled and should be the second key on the bottom row of the keyboard.
- ReturnKey — This should be placed on the end of the bottom row.
- ShiftKey — This causes the CharKeys to toggle between their normal and their shifted states. It should be the first key on the third row.
- SpaceKey — The space key dynamically expands its width to fill in any spare space and should be placed roughly in the middle of the bottom row. The exact positioning will typically vary between layouts as additional keys may be present (such as the UrlKey, or the ‘@’ key for email layouts)
- SymbolShiftKey — This toggles switching between the character layout and the symbol layout, and should be the first key on the bottom row.
- UrlKey — This key displays common URL endings specific to your locale’s layout (e.g. «.se» for Swedish layouts, «.de» for German). This key should only appear on the email, url and url_search layouts and should be displayed on the bottom row to the right of the space key. The UrlKey can also display extended keys, which can be used to provide any additional domains common to your locale.
If you used the template to create your keyboard then most of the special keys should be setup in their correct positions already, you’ll just need to add entries for all of the CharKeys and modify the UrlKey to match your language.
Letting the keyboard know about our plugin
Predictions
Keyboard predictions are based around the presage prediction engine. Presage can learn from user input, however to bootstrap the learning so that predictions are available when the user first starts using the keyboard, we train presage using an appropriately licensed ebook for that language (typically we’ve been selecting ebooks from Project Gutenburg).
If you used the template you’ll find an empty file called «free_ebook.txt» in your layout plugin’s src/ directory. Simply replace this file with appropriate sample text and then run «qmake && make lang_db_cy» (replacing «cy» with the language code of the layout you’re creating).
How can one create a custom keyboard layout for Linux?
Ideally, I’m looking for a GUI tool, maybe a web application, but good documentation on creating a text definition for a keyboard layout would be great too. What is the normal procedure here? In this question, the asker already decided on using «setxkbmap». Is this the recommended way? Can I add the layout to eg a git repo so I can make changes to it over time if I need to?
1 Answer 1
You can edit keymaps with XKeyCaps. On the upside, XKeyCaps is a GUI and it’s very mature. A downside of XKeyCaps is that it’s only a front-end to the old-style X window keymap interface, xmodmap, and not the modern interface, XKB.
It’s perfectly fine to use xmodmap if you want, but it isn’t as powerful as XKB. XKB features that xmodmap lack include:
- A better handling of modifiers. X11 has many quirks related to modifiers, including the fact that CapsLock and NumLock are considered modifiers. An example of a thing that I do with XKB that doesn’t work with xmodmap is that I make Alt + CapsLock be Lock (that’s how X11 spells Caps Lock), but not CapsLock itself. Another thing xmodmap can’t do is to have different sets of modifiers influence different keys, for example to have Shift and AltGr affect printable characters but Ctrl and Alt affect function keys.
- XKB is made of composable modules. Xmodmap is monolithic. This is great for people who want to combine predefined settings (“give me a Hungarian keyboard with Ctrl and CapsLock swapped and Alt + Windows is Menu , and for the key codes of a Sun 5 keyboard”), but you wouldn’t care since you’re making your own layout.
- If you hotplug a keyboard, the XKB system defaults get reapplied. It’s also an XKB limitation since the system defaults are reapplied, not your user settings.
- XKB has better support for non-alphabetic languages or multi-script input. I can’t give details because this is not a topic that I know well.
- There are tools to quickly switch between layouts (for example to have window-specific layouts) or to indicate the current keyboard layout that only work with XKB.
- I’m not sure if Wayland supports xmodmap.
There are a few XKB GUI editors on the web, but all the ones I can find are projects that were quickly abandoned and aren’t as easy to install as XKeyCaps. I can’t find one with an Ubuntu package, for instance.
If you decide to write the keymap manually, the advantage of xmodmap is that it’s very simple. You can read the man page, maybe look at an example, and you’ll quickly understand everything except the subtlety of modifiers. If you can’t figure out how to do it with xmodmap, it’s probably impossible. The advantage of XKB, once again, is that it’s more powerful. The downsides of XKB are that it’s complex and poorly documented. Some suggested reading if you want to go this route:
- As always, the Arch Wiki.
- XKB questions here on Unix Stack Exchange.
- An Unreliable Guide to XKB Configuration, despite its name and age, is one of the best resources on the subject. How does xkb compilation work? may be a useful complement.
- Other resources linked from the X11 wiki may or may not be helpful.
- xkbcomp -a :0 current.xkb dumps your current layout to a file.
Whichever way you choose, the keymap source is a text file or a collection of text files which works well with version control.