# The XKB keymap text format, V1 {#keymap-text-format-v1}
This document describes the `XKB_KEYMAP_FORMAT_TEXT_V1` [keymap] format,
as implemented by libxkbcommon.
@see For an overview of the role of this format, please see “@ref xkb-the-config ""”.
@see For examples of keymaps in this format, please see “@ref user-configuration ""”.
For further examples see [xkeyboard-config], the standard database of keyboard
configuration data.
@note Due to the complexity of the format, this document is still is construction.
Some additional resources are:
@note
- [Ivan Pascal’s XKB documentation][ivan-pascal]
- [An Unreliable Guide to XKB Configuration][unreliable-guide]
- [The X Keyboard Extension: Protocol Specification][XKB Protocol]
- [How to enhance XKB configuration][xkeyboard-config doc]
- [ArchWiki XKB page][arch-wiki]
[xkeyboard-config]: https://gitlab.freedesktop.org/xkeyboard-config/xkeyboard-config
[ivan-pascal]: https://web.archive.org/web/20190724015820/http://pascal.tsu.ru/en/xkb/
[unreliable-guide]: https://www.charvolant.org/doug/xkb/html/index.html
[XKB Protocol]: https://www.x.org/releases/current/doc/kbproto/xkbproto.html
[xkeyboard-config doc]: https://gitlab.freedesktop.org/xkeyboard-config/xkeyboard-config/-/blob/master/docs/README.enhancing
[arch-wiki]: https://wiki.archlinux.org/index.php/X_keyboard_extension
@tableofcontents{html:2}
[terminology]: @ref terminology
[introduction]: @ref introduction
[xkb_keymap]: @ref the-xkb_keymap-block
[xkb_keycodes]: @ref the-xkb_keycodes-section
[xkb_types]: @ref the-xkb_types-section
[xkb_compat]: @ref the-xkb_compat-section
[xkb_symbols]: @ref the-xkb_symbols-section
[virtual modifier statements]:@ref virtual-modifier-statements
[modifiers bindings]: @ref modifiers-bindings
[actions]: @ref key-actions
## Terminology {#terminology}
- Keycode
-
-
Code that identifies a physical key on a keyboard.
- _Raw_ keycodes are the numeric identifiers used as input in XKB.
They are the result of the low-level processing of the data that
keyboards send to a computer. For instance `36` may represent
the return key.
- _XKB_ keycodes are _symbolic_ names assigned to raw keycodes in
order to facilitate their mapping to symbols. For instance the
keycode for the return key is the abbreviation `RTRN`.
See [xkb_keycodes] for further details.
- Symbols
-
A _keysym_ (short for “key symbol”) is a numeric encoding of a
symbol on the cap of a key.
They have a canonical name for convenience. It can be:
- A _character:_ e.g. `a` and `A` for Latin scripts,
`alpha` “α” and `ALPHA` “Α” for Greek, etc.
- A _dead key:_ e.g. `dead_grave` and `dead_diaeresis`, corresponding
respectively to the [grave accent](https://en.wikipedia.org/wiki/Grave_accent)
and the [diaeresis](https://en.wikipedia.org/wiki/Diaeresis_%28diacritic%29)
diacritics.
A [dead key](https://en.wikipedia.org/wiki/Dead_key) is a special kind of key
that does not generate a character by itself, but modifies the character
generated by the key struck(s) immediately after.
- A [modifier]: e.g. `Shift_L`, `Control_R`, `Caps_Lock`.
See hereinafter.
- A _system action:_ e.g. the arrow `Left`, `Pause`, `Escape`, `F1`.
The complete list of keysyms is defined in `xkbcommon/xkbcommon-keysyms.h`.
See [xkb_symbols] for further details on binding keysyms to keycodes.
- Modifier
-
A _modifier key_ is a key that modifies the effect of other keys:
e.g. Shift, Control, Caps Lock, etc.
The state of a modifier key (active/inactive) is encoded as a
_modifier index_ (or modifier bit or simply modifier) and has
an associated _unique name_.
For historical reasons, modifiers are divided in two categories:
- Real modifiers
-
They are the 8 _predefined_ (AKA core, X11) modifiers
(see [usual modifiers] hereinafter).
Real modifiers ensure backward compatibility: indeed
they are the actual bits used to compute the [levels][level]
and are communicated via the API of xkbcommon.
Some are generic modifiers (`Mod[1-5]`) that do not have a
conventional interpretation and are the motivation of the
introduction of [virtual modifiers].
- Virtual modifiers
-
They are the modifiers that are _not_ predefined.
Each modifier defines a _mapping_ to one or multiple
_real_ modifier. Real modifiers map to themselves.
The following table lists the
usual modifiers
present in the [standard keyboard configuration][xkeyboard-config].
Note that this is provided for information only, as it may change
depending on the user configuration.
| Modifier | Type | Usual mapping | Comment |
| ------------ | ------- | ------------- | --------------------------- |
| `Shift` | Real | `Shift` | The usual [Shift] |
| `Lock` | Real | `Lock` | The usual [Caps Lock][Lock] |
| `Control` | Real | `Control` | The usual [Control] |
| `Mod1` | Real | `Mod1` | Not conventional |
| `Mod2` | Real | `Mod2` | Not conventional |
| `Mod3` | Real | `Mod3` | Not conventional |
| `Mod4` | Real | `Mod4` | Not conventional |
| `Mod5` | Real | `Mod5` | Not conventional |
| `Alt` | Virtual | `Mod1` | The usual [Alt] |
| `Meta` | Virtual | `Mod1` or `Mod4` | The legacy [Meta] key |
| `NumLock` | Virtual | `Mod2` | The usual [NumLock] |
| `Super` | Virtual | `Mod4` | The usual [Super]/GUI |
| `LevelThree` | Virtual | `Mod3` | [ISO][ISO9995] level 3, aka [AltGr] |
| `LevelFive` | Virtual | `Mod5` | [ISO][ISO9995] level 5 |
[usual modifiers]: @ref usual-modifiers
[Shift]: https://en.wikipedia.org/wiki/Control_key
[Lock]: https://en.wikipedia.org/wiki/Caps_Lock
[Control]: https://en.wikipedia.org/wiki/Control_key
[Alt]: https://en.wikipedia.org/wiki/Alt_key
[AltGr]: https://en.wikipedia.org/wiki/AltGr_key
[NumLock]: https://en.wikipedia.org/wiki/Num_Lock
[Meta]: https://en.wikipedia.org/wiki/Meta_key
[Super]: https://en.wikipedia.org/wiki/Super_key_(keyboard_button)
A modifier key can report its state in one of the following 3 ways:
- Depressed
- Active while depressed; e.g. the usual Shift.
- Latched
-
Activated when pressed and deactivated after the next
non-modifier key press.
- Locked
-
Activated when pressed and deactivated when pressed again;
e.g. the usual Caps Lock.
See [modifiers bindings] for further details.
[depressed]: @ref depressed-mod-def
[latched]: @ref latched-mod-def
[locked]: @ref locked-mod-def
- Shift Level
-
A key may produce different
results depending of the active modifiers: e.g. for a Latin script,
pressing the key A produces “a” and holding Shift while pressing A
produces “A”.
This various results are organized in an ordered list; the _index_
of each entry is called a shift level
or simply level. By convention the lowest level is the result when
no modifier is active.
Example for the key `A` on a latin script keyboard:
| Level | Description | Keysym | Active key modifiers |
|-------|--------------------------------|--------|----------------------|
| 1 | Lower case letters | `a` | None |
| 2 | Upper case letters. | `A` | `Shift` |
| 3 | Alternative lower case letters | `ae` | `AltGr` |
| 4 | Alternative upper case letters | `AE` | `Shift` + `AltGr` |
A key shift level is the logical _state_ of a key corresponding to
the current shift level it used.
Key shift levels are derived from the modifiers states, but not
necessarily in the same way for all keys. For example, for Latin
script the Caps Lock modifier selects the level 2 for alphabetic
keys such as `A` but has no effect on a numeric key.
There are groups of keys with the same characteristics: letters,
punctuation, numeric keypad, etc. The meaning of their levels is
identical and thus can be shared: this generalization is called
a _key type_ (see hereinafter).
- Key type
-
A key type defines the levels available for a key and
how to derive the active level from the modifiers states. Examples:
- `ONE_LEVEL`: the key has only one level, i.e. it is not affected
by any modifiers. Example: the modifiers themselves.
- [`TWO_LEVEL`][TWO_LEVEL]: the key has two levels:
- Level 1: default level, active when the `Shift` modifier is _not_ active.
- Level 2: level activated with the `Shift` modifier.
- [`FOUR_LEVEL`][FOUR_LEVEL]: see the example in the previous section.
See [xkb_types] for further details.
- Layout
-
A mapping of keycodes to symbols, actions and key types.
A user who deals with multiple languages may need two or more
different layouts: e.g. a layout for Arabic and another one for
English. In this context, layouts are called _groups_ in XKB,
as defined in the [standard ISO/IEC 9995][ISO9995].
Layouts are ordered and identified by their index. Example:
- Layout 1: Arabic
- Layout 2: English
- Key Action
-
In XKB world, a key action defines the effect a key
has on the state of the keyboard or the state of the display server.
Examples:
- Change the state of a modifier.
- Change the active group.
- Move the mouse pointer.
See the section “[Key actions][actions]” for further details.
- Indicator
-
A keyboard indicator is a mean to report a specific aspect of the
keyboard state.
- Physical indicator
-
Typically a labelled LED on the keyboard, e.g. “Caps Lock” and
“Num Lock”.
- Logical indicator
-
A customizable derived state of the keyboard.
Its changes creates events that can be monitored.
There are two categories:
- _Real_ indicators are those associated to a physical indicator.
For example, the “Caps Lock” logical modifier controls the
corresponding physical LED.
Because indicators are customizable, if one misses a “Num Lock”
LED, one could define instead the “Caps Lock” _indicator_ to
activate its LED when the “Num Lock” _modifier_ is active.
- _Virtual_ indicators are not associated to a physical indicator.
Their effect is only visible for programs monitoring them.
Note that the meanings of _real_ and _virtual_ is slightly
different than the one used for [modifier].
See: [xkb_keycodes][indicator name] to define indicators and
[xkb_compat][indicator effect] to define their effects.
- Keymap
-
The _complete_ definition of the
mapping of raw keycodes to symbols and actions.
It fully defines the behavior of a keyboard.
Depending of the context, a keymap may refer to:
- the software object defined and managed by libxkbcommon;
- the text configuration used to create this software object.
See @ref keymap-components-intro and [xkb_keymap] for further details.
- Keyboard configuration database @anchor database-def
-
A database that provides the [keymap components](@ref keymap-components-intro).
\*nix OSs uses the _standard_ database [xkeyboard-config]. One may extend
this database with _custom_ layouts: see “@ref user-configuration ""” for
further details.
[keycode]: @ref keycode-def
[keysym]: @ref keysym-def
[keysyms]: @ref keysym-def
[modifier]: @ref modifier-def
[modifiers]: @ref modifier-def
[real modifier]: @ref real-modifier-def
[real modifiers]: @ref real-modifier-def
[virtual modifier]: @ref virtual-modifier-def
[virtual modifiers]: @ref virtual-modifier-def
[level]: @ref level-def
[shift level]: @ref level-def
[level index]: @ref level-index-def
[key type]: @ref key-type-def
[key types]: @ref key-type-def
[layout]: @ref layout-def
[action]: @ref key-action-def
[indicator]: @ref indicator-def
[keymap]: @ref keymap-def
[database]: @ref database-def
[ISO9995]: https://en.wikipedia.org/wiki/ISO/IEC_9995
## Introduction to the XKB text format {#introduction}
The XKB text format uses a syntax similar to the [C programming language][C].
Note that the similarity with C stops here: the XKB text format is only a
_configuration_ format and is not intended for programming.
The XKB text format is used to configure a _keyboard keymap_, which is
introduced in “@ref xkb-the-config ""”. It has the following two main use cases,
illustrated in the [diagram hereinafter](@ref xkb-keymap-components-diagram):
- __Server:__ Load a keymap from the keymap configuration database, then handle
input events by updating the keyboard state. The keymap is assembled from
an [RMLVO configuration][RMLVO] and its corresponding
[KcCGST components][KcCGST] files.
@see xkb_keymap::xkb_keymap_new_from_names
@see [xkeyboard-config] for the implementation of the *standard* keymap
configuration database.
@see “@ref user-configuration ""” to add a *custom* layout or option.
- __Client:__ Load the active keymap from the server, then handle update events
sent by the server. The [complete keymap] is directly
available in a _self-contained_ file.
@see xkb_keymap::xkb_keymap_new_from_string
@anchor xkb-keymap-components-diagram
@dotfile xkb-keymap-components "XKB text format use cases"
[C]: https://en.wikipedia.org/wiki/C_(programming_language)#Syntax
[RMLVO]: @ref RMLVO-intro
[KcCGST]: @ref KcCGST-intro
[complete keymap]: @ref keymap-intro
### XKB file
There are two kinds of files for the XKB text format:
- Keymap file @anchor keymap-file-def
-
A file with the _complete_ description of the [keymap] object.
It is the kind of file that the server sends to the client (see
the [diagram](@ref xkb-keymap-components-diagram) above).
Its top-level structure consists of the [xkb_keymap] block.
- Keymap _component_ file @anchor keymap-component-file-def
-
A file with the description of a _particular_ [KcCGST component][KcCGST].
It is the kind of file that the server uses to assemble a [keymap file].
Its top-level structure consists of a _single type_ of [keymap sections].
A component file may contain multiple such sections.
[keymap file]: @ref keymap-file-def
[keymap component file]: @ref keymap-component-file-def
[keymap sections]: @ref keymap-section-def
[section]: @ref keymap-section-def
[keymap components]: @ref keymap-component-def
### Keymap components {#keymap-components-intro}
[Keymap components][keymap components] are described with [keymap sections].
They are grouped in [keymap component files][keymap component file] to form a
[keyboard configuration database][database].
- Keymap _component_ @anchor keymap-component-def
-
A part of the keymap _object_. The set of keymap components is referred as
[KcCGST]. They are presented in the [table hereinafter][keymap components table].
- Keymap _section_ @anchor keymap-section-def
-
A part of the keymap _text configuration_ dedicated to one of the
[keymap components][keymap components table].
- Component _folder_
-
A folder in the [keymap configuration database][database], dedicated to files
with partial definitions of the same keymap section.
[keymap components table]: @ref keymap-components-table
@anchor keymap-components-table
Keymap components
| [Component](@ref keymap-component-def) |
[Section][section] in a [keymap][xkb_keymap] |
Folder in a keymap configuration database |
Description |
| Key codes |
[xkb_keycodes] |
`keycodes` |
A translation of the raw [key codes][keycode] from the keyboard into
symbolic names.
|
| Compatibility |
[xkb_compat] |
`compat` |
A specification of what internal actions modifiers and various
special-purpose keys produce.
|
| (Geometry) |
xkb_geometry |
`geometry` |
A description of the physical layout of a keyboard.
@attention This legacy feature is [not supported](@ref geometry-support)
by _xkbcommon_.
|
| Key symbols |
[xkb_symbols] |
`symbols` |
A translation of symbolic [key codes][keycode] into actual [key symbols][keysyms]
(keysyms).
|
| Key types |
[xkb_types] |
`types` |
Types describe how a pressed key is affected by active [modifiers]
such as Shift, Control, Alt, etc.
|
### Comments
Comments are introduced following either `//` or `#` until the end of the line.
### Literals
- String literal
-
A string is surrounded by double quotes: “
"”.
The following _escape sequences_ are supported:
| Escape sequence | Meaning |
| ------------------ | ------------------------------------------------------- |
| `\\` | Backslash “`\`” |
| `\b` | Backspace |
| `\e` | Escape |
| `\f` | Form feed |
| `\n` | Line feed (newline) |
| `\r` | Carriage return |
| `\t` | Horizontal tabulation |
| `\v` | Vertical tabulation |
| `\` + octal number | Corresponding ASCII character: `\0` → NULL, `\42` → `"` |
@note The string _encoding_ is unspecified and not validated, but for best
results, stick to ASCII.
- Number literal
-
A number can be written in three forms:
- _decimal integer:_ `1`, `123`, etc.
- _decimal floating-point number:_ `1.23`, etc.
- _hexadecimal integer:_ prefixed with `0x`: `0x123`, `0xff`, `0xAB`, etc.
### Keywords
The following table presents the keywords used in the format. They are
_case-sensitive_.
| Keyword | Use |
| ----------------------- | ------------------------------ |
| `action` | TODO |
| `alias` | TODO |
| `alphanumeric_keys` | TODO |
| `alternate_group` | TODO |
| `alternate` | TODO |
| `augment` | Mode qualifier for [include] statements |
| `default` | TODO |
| `function_keys` | TODO |
| `group` | TODO |
| `hidden` | TODO |
| `include` | [Include statement][include] |
| `indicator` | TODO |
| `interpret` | TODO |
| `key` | TODO |
| `keypad_keys` | TODO |
| `keys` | TODO |
| `logo` | TODO |
| `mod_map` | Alias of `modifier_map` |
| `modifier_keys` | TODO |
| `modmap` | Alias of `modifier_map` |
| `modifier_map` | TODO |
| `outline` | TODO |
| `overlay` | TODO |
| `override` | Mode qualifier for [include] statements |
| `partial` | TODO |
| `replace` | Mode qualifier for [include] statements |
| `row` | TODO |
| `section` | TODO |
| `shape` | TODO |
| `solid` | TODO |
| `text` | TODO |
| `type` | TODO |
| `virtual_modifiers` | TODO |
| `virtual` | TODO |
| `xkb_compat_map` | Alias of `xkb_compatibility_map` |
| `xkb_compat` | Alias of `xkb_compatibility_map` |
| `xkb_compatibility_map` | Declare a [compatibility section][xkb_compat] |
| `xkb_compatibility` | Alias of `xkb_compatibility_map` |
| `xkb_geometry` | Declare a geometry section (TODO: legacy) |
| `xkb_keycodes` | Declare a [keycodes section][xkb_keycodes] |
| `xkb_keymap` | Declare a [keymap block][xkb_keymap] |
| `xkb_layout` | TODO |
| `xkb_semantics` | TODO |
| `xkb_symbols` | Declare a [symbols section][xkb_symbols] |
| `xkb_types` | Declare a [key types section ][xkb_types] |
[include]: @ref xkb-include
### Built-in settings
There are many built-in settings; they are explained in the following relevant
sections.
These settings are _case-insensitive_, e.g. the following strings denote
the same key word: `SETMODS`, `SetMods`, `setMods` and `setmods`.
### The include mechanism {#xkb-include}
@todo the import mechanism, its qualifiers
## The “xkb_keymap” block {#the-xkb_keymap-block}
A [keymap file] consists of a single top-level `xkb_keymap`
block, under which are nested the following sections:
[xkb_keycodes]
-
A translation of the hardware/evdev scancodes from the keyboard into
XKB symbolic keycodes.
[xkb_types]
-
A specification of the modifier mask, target level and preserved
modifiers various modifiers combination produce.
[xkb_compat]
-
A specification of what actions various special-purpose keys produce.
[xkb_symbols]
-
A translation of symbolic key codes into actual symbols and actions.
Overview of a keymap file:
```c
xkb_keymap {
xkb_keycodes "XXX" {
// ...
}
xkb_types "XXX" {
// ...
};
xkb_compatibility "XXX" {
// ...
};
xkb_symbols "XXX" {
// ...
};
};
```
## The “xkb_keycodes” section {#the-xkb_keycodes-section}
This is the simplest [section] type, and is the first one to be
compiled. The purpose of this is mostly to map between the
hardware/evdev scancodes and XKB [keycodes][keycode]. Each key is given a name
by which it can be referred to later, e.g. in the symbols section.
### Keycode statements
Statements of the form:
= 49;
= 10;
The above would let 49 and 10 be valid keycodes in the keymap, and
assign them the names `TLDE` and `AE01` respectively. The format
`` is always used to refer to a key by name.
The naming convention `` is based on the
[standard ISO/IEC 9995-1][ISO9995-1]. It denotes the position of the
key in the keyboard grid. It means: the main alphanumeric section
(`A`), row `E` and column `01`.
The following figure illustrates the grid on a staggered standard
US QWERTY keyboard. `` corresponds to the key `1`.
```
\ 99 \ 00 \ 01 \ 02 \ 03 \ 04 \ 05…
\ \ \ \ \ \ \
-----------------------------------------
E \ \ ^ \ 1 \ 2 \ 3 \ 4 \ 5…
------------------------------------------
D \ Tab \ Q \ W \ E \ R \ T…
-------------------------------------------
C \Caps \ A \ S \ D \ F \ G…
--------------------------------------------
B \Shift \ Z \ X \ C \ V \ B…
---------------------------------------------
A \Ctrl\GUI \Alt \Space…
----------------------------------------------
```
[ISO9995-1]: https://en.wikipedia.org/wiki/ISO/IEC_9995#ISO/IEC_9995-1
In the common case this just maps to the evdev scancodes from
`/usr/include/linux/input.h`, e.g. the following definitions:
#define KEY_GRAVE 41
#define KEY_1 2
correspond to the ones above. Similar definitions appear in the
xf86-input-keyboard driver. Note that in all current keymaps there’s a
constant offset of 8 (for historical reasons).
Note that contrary to xkbcommon, the X11 protocol supports keycodes
only up to `255`. Therefore, when interfacing with X11, keymaps and applications
using keycodes beyond `255` should expect warnings.
If there’s a conflict, like the same name given to different keycodes,
or same keycode given different names, it is resolved according to the
merge mode which applies to the definitions.
### Alias statements
Statements of the form:
alias