1# The XKB keymap text format, V1 2 3This document describes the `XKB_KEYMAP_FORMAT_TEXT_V1` keymap format, 4as implemented by libxkbcommon. 5 6NOTE: This document is ever incomplete. Some additional resources are: 7 8- [Ivan Pascal's XKB documentation](https://web.archive.org/web/20190724015820/http://pascal.tsu.ru/en/xkb/) 9- [An Unreliable Guide to XKB Configuration](https://www.charvolant.org/doug/xkb/html/index.html) 10- [ArchWiki XKB page](https://wiki.archlinux.org/index.php/X_keyboard_extension) 11 12A keymap consists of a single top-level `xkb_keymap` block, underwhich 13are nested the following sections. 14 15 16## The `xkb_keycodes` section 17 18This is the simplest section type, and is the first one to be 19compiled. The purpose of this is mostly to map between the 20hardware/evdev scancodes and xkb keycodes. Each key is given a name 21by which it can be referred to later, e.g. in the symbols section. 22 23### Keycode statements 24 25Statements of the form: 26 27 <TLDE> = 49; 28 <AE01> = 10; 29 30The above would let 49 and 10 be valid keycodes in the keymap, and 31assign them the names `TLDE` and `AE01` respectively. The format 32`<WXYZ>` is always used to refer to a key by name. 33 34[The naming convention `<AE01>` just denotes the position of the key 35in the main alphanumeric section of a standard QWERTY keyboard, with 36the two letters specifying the row and the two digits specifying the 37column, from the bottom left.] 38 39In the common case this just maps to the evdev scancodes from 40`/usr/include/linux/input.h`, e.g. the following definitions: 41 42 #define KEY_GRAVE 41 43 #define KEY_1 2 44 45correspond to the ones above. Similar definitions appear in the 46xf86-input-keyboard driver. Note that in all current keymaps there's a 47constant offset of 8 (for historical reasons). 48 49If there's a conflict, like the same name given to different keycodes, 50or same keycode given different names, it is resolved according to the 51merge mode which applies to the definitions. 52 53### Alias statements 54 55Statements of the form: 56 57 alias <MENU> = <COMP>; 58 59Allows to refer to a previously defined key (here `<COMP>`) by another 60name (here `<MENU>`). Conflicts are handled similarly to keycode 61statements. 62 63### LED name statements 64 65Statements of the form: 66 67 indicator 1 = "Caps Lock"; 68 indicator 2 = "Num Lock"; 69 indicator 3 = "Scroll Lock"; 70 71Assigns a name to the keyboard LED (AKA indicator) with the given 72index. The LED may be referred by this name later in the compat 73section and by the user. 74 75 76## The `xkb_types` section 77 78This section is the second to be processed, after `xkb_keycodes`. 79However, it is completely independent and could have been the first to 80be processed (it does not refer to specific keys as specified in the 81`xkb_keycodes` section). 82 83This section defines key types, which, given a key and a keyboard 84state (i.e. modifier state and group), determine the shift level to be 85used in translating the key to keysyms. These types are assigned to 86each group in each key, in the `xkb_symbols` section. 87 88Key types are called this way because, in a way, they really describe 89the "type" of the key (or more correctly, a specific group of the 90key). For example, an ordinary keymap will provide a type called 91`KEYPAD`, which consists of two levels, with the second level being 92chosen according to the state of the Num Lock (or Shift) modifiers. 93Another example is a type called `ONE_LEVEL`, which is usually 94assigned to keys such as Escape; these have just one level and are not 95affected by the modifier state. Yet more common examples are 96`TWO_LEVEL` (with Shift choosing the second level), `ALPHABETIC` 97(where Caps Lock may also choose the second level), etc. 98 99### Type definitions 100 101Statements of the form: 102 103 type "FOUR_LEVEL" { ... } 104 105The above would create a new type named `FOUR_LEVEL`. 106The body of the definition may include statements of the following 107forms: 108 109#### `level_name` statements 110 111 level_name[Level1] = "Base"; 112 113Mandatory for each level in the type. 114 115Gives each level in this type a descriptive name. It isn't used 116for anything. 117 118Note: A level may be specified as Level[1-8] or just a number (can 119be more than 8). 120 121#### `modifiers` statement 122 123 modifiers = Shift+Lock+LevelThree; 124 125Mandatory, should be specified only once. 126 127A mask of real and virtual modifiers. These are the only modifiers 128being considered when matching the modifier state against the type. 129The other modifiers, whether active or not, are masked out in the 130calculation. 131 132#### `map` entry statements 133 134 map[Shift+LevelThree] = Level4; 135 136Should have at least as many mappings as there are levels in the type. 137 138If the active modifiers, masked with the type's modifiers (as stated 139above), match (i.e. equal) the modifiers inside the `map[]` statement, 140then the level in the right hand side is chosen. For example, in the 141above, if in the current keyboard state the `Shift` and `LevelThree` 142modifiers are active, while the `Lock` modifier is not, then the 143keysym(s) in the 4th level of the group will be returned to the user. 144 145#### `preserve` statements 146 147 map[Shift+Lock+LevelThree] = Level5; 148 preserve[Shift+Lock+LevelThree] = Lock; 149 150When a key type is used for keysym translation, its modifiers are said 151to be "consumed". For example, in a simple US keymap, the "g" "g" key 152is assigned an ordinary `ALPHABETIC` key type, whose modifiers are 153Shift and Lock; then for the "g" key, these two modifiers are consumed 154by the translation. This information is relevant for applications 155which further process the modifiers, since by then the consumed 156modifiers have already "done their part" and should be masked out. 157 158However, sometimes even if a modifier had already affected the key 159translation through the type, it should *not* be reported as consumed, 160for various reasons. In this case, a `preserve[]` statement can be 161used to augment the map entry. The modifiers inside the square 162brackets should match one of the map[] statements in the type (if 163there is no matching map entry, one mapping to Level1 is implicitly 164added). The right hand side should consists of modifiers from the 165type's modifiers; these modifiers are then "preserved" and not 166reported as consumed. 167 168 169## The `xkb_compat` section 170 171This section is the third to be processed, after `xkb_keycodes` and 172`xkb_types`. 173 174### Interpret statements 175 176Statements of the form: 177 178 interpret Num_Lock+Any { ... } 179 interpret Shift_Lock+AnyOf(Shift+Lock) { ... } 180 181The `xkb_symbols` section (see below) allows the keymap author to 182perform, among other things, the following things for each key: 183 184- Bind an action, like SetMods or LockGroup, to the key. Actions, like 185 symbols, are specified for each level of each group in the key 186 separately. 187 188- Add a virtual modifier to the key's virtual modifier mapping 189 (vmodmap). 190 191- Specify whether the key should repeat or not. 192 193However, doing this for each key (or level) is tedious and inflexible. 194Interpret's are a mechanism to apply these settings to a bunch of 195keys/levels at once. 196 197Each interpret specifies a condition by which it attaches to certain 198levels. The condition consists of two parts: 199 200- A keysym. If the level has a different (or more than one) keysym, 201 the match fails. Leaving out the keysym is equivalent to using the 202 `NoSymbol` keysym, which always matches successfully. 203 204- A modifier predicate. The predicate consists of a matching operation 205 and a mask of (real) modifiers. The modifiers are matched against 206 the key's modifier map (modmap). The matching operation can be one 207 of the following: 208 209 * `AnyOfOrNone` - The modmap must either be empty or include at 210 least one of the specified modifiers. 211 * `AnyOf` - The modmap must include at least one of the specified 212 modifiers. 213 * `NoneOf` - The modmap must not include any of the specified 214 modifiers. 215 * `AllOf` - The modmap must include all of the specified modifiers 216 (but may include others as well). 217 * `Exactly` - The modmap must be exactly the same as the specified 218 modifiers. 219 220 Leaving out the predicate is equivalent to using `AnyOfOrNone` while 221 specifying all modifiers. Leaving out just the matching condition is 222 equivalent to using `Exactly`. 223 224An interpret may also include `useModMapMods = level1;` - see below. 225 226If a level fulfils the conditions of several interprets, only the 227most specific one is used: 228 229- A specific keysym will always match before a generic `NoSymbol` 230 condition. 231 232- If the keysyms are the same, the interpret with the more specific 233 matching operation is used. The above list is sorted from least to 234 most specific. 235 236- If both the keysyms and the matching operations are the same (but the 237 modifiers are different), the first interpret is used. 238 239As described above, once an interpret "attaches" to a level, it can bind 240an action to that level, add one virtual modifier to the key's vmodmap, 241or set the key's repeat setting. You should note the following: 242 243- The key repeat is a property of the entire key; it is not 244 level-specific. In order to avoid confusion, it is only inspected 245 for the first level of the first group; the interpret's repeat 246 setting is ignored when applied to other levels. 247 248- If one of the above fields was set directly for a key in 249 `xkb_symbols`, the explicit setting takes precedence over the 250 interpret. 251 252The body of the statement may include statements of the following 253forms (all of which are optional): 254 255#### `useModMapMods` statement 256 257 useModMapMods = level1; 258 259When set to `level1`, the interpret will only match levels which are 260the first level of the first group of the keys. This can be useful in 261conjunction with e.g. a `virtualModifier` statement. 262 263#### `action` statement 264 265 action = LockMods(modifiers=NumLock); 266 267Bind this action to the matching levels. 268 269#### `virtualModifier` statement 270 271 virtualModifier = NumLock; 272 273Add this virtual modifier to the key's vmodmap. The given virtual 274modifier must be declared at the top level of the file with a 275`virtual_modifiers` statement, e.g.: 276 277 virtual_modifiers NumLock; 278 279#### `repeat` statement 280 281 repeat = True; 282 283Set whether the key should repeat or not. Must be a boolean value. 284 285### LED map statements 286 287Statements of the form: 288 289 indicator "Shift Lock" { ... } 290 291This statement specifies the behavior and binding of the LED (AKA 292indicator) with the given name ("Shift Lock" above). The name should 293have been declared previously in the `xkb_keycodes` section (see LED 294name statement), and given an index there. If it wasn't, it is created 295with the next free index. 296 297The body of the statement describes the conditions of the keyboard 298state which will cause the LED to be lit. It may include the following 299statements: 300 301#### `modifiers` statement 302 303 modifiers = ScrollLock; 304 305If the given modifiers are in the required state (see below), the 306LED is lit. 307 308#### `whichModState` statement 309 310 whichModState = Latched+Locked; 311 312Can be any combination of: 313 314* `base`, `latched`, `locked`, `effective` 315* `any` (i.e. all of the above) 316* `none` (i.e. none of the above) 317* `compat` (legacy value, treated as effective) 318 319This will cause the respective portion of the modifier state (see 320`struct xkb_state`) to be matched against the modifiers given in the 321`modifiers` statement. 322 323Here's a simple example: 324 325indicator "Num Lock" { 326 modifiers = NumLock; 327 whichModState = Locked; 328}; 329 330Whenever the NumLock modifier is locked, the Num Lock LED will light 331up. 332 333#### `groups` statement 334 335 groups = All - group1; 336 337If the given groups are in the required state (see below), the LED is 338lit. 339 340#### `whichGroupState` statement 341 342 whichGroupState = Effective; 343 344Can be any combination of: 345 346* `base`, `latched`, `locked`, `effective` 347* `any` (i.e. all of the above) 348* `none` (i.e. none of the above) 349 350This will cause the respective portion of the group state (see 351`struct xkb_state`) to be matched against the groups given in the 352`groups` statement. 353 354Note: the above conditions are disjunctive, i.e. if any of them are 355satisfied the LED is lit. 356 357 358## The `xkb_symbols` section 359 360NOTE: The documentation of this section is incomplete. 361 362This section is the fourth to be processed, after `xkb_keycodes`, `xkb_types` 363and `xkb_compat`. 364 365Statements of the form: 366 367 xkb_symbols "basic" { 368 ... 369 } 370 371Declare a symbols map named `basic`. Statements inside the curly braces only 372affect the symbols map. 373 374A map can have various flags applied to it above the statement, separated by 375whitespace: 376 377 partial alphanumeric_keys 378 xkb_symbols "basic" { 379 ... 380 } 381 382The possible flags are: 383 384 * `partial` - Indicates that the map doesn't cover a complete keyboard. 385 * `default` - Marks the symbol map as the default map in the file when no 386 explicit map is specified. If no map is marked as a default, the first map 387 in the file is the default. 388 * `hidden` - Variant that can only be used internally 389 * `alphanumeric_keys` - Indicates that the map contains alphanumeric keys 390 * `modifier_keys` - Indicates that the map contains modifier keys 391 * `keypad_keys` - Indicates that the map contains keypad keys 392 * `function_keys` - Indicates that the map contains function keys 393 * `alternate_group` - Indicates that the map contains keys for an alternate 394 group 395 396If no `*_keys` flags are supplied, then the map is assumed to cover a complete 397keyboard. 398 399At present, except for `default`, none of the flags affect key processing in 400libxkbcommon, and only serve as metadata. 401 402### Name statements 403 404Statements of the form: 405 406 name[Group1] = "US/ASCII"; 407 groupName[1] = "US/ASCII"; 408 409Gives the name "US/ASCII" to the first group of symbols. Other groups can be 410named using a different group index (ex: `Group2`), and with a different name. 411A group must be named. 412 413`group` and `groupName` mean the same thing, and the `Group` in `Group1` is 414optional. 415 416### Include statements 417 418Statements of the form: 419 420 include "nokia_vndr/rx-51(nordic_base) 421 422Will include data from another `xkb_symbols` section, possibly located in 423another file. Here it would include the `xkb_symbols` section called 424`nordic_base`, from the file `rx-51` located in the `nokia_vndr` folder, itself 425located in an XKB include path. 426 427### Key statement 428 429Statements of the form: 430 431 key <AD01> { [ q, Q ] }; 432 433Describes the mapping of a keycode `<AD01>` to a given group of symbols. The 434possible keycodes are the keycodes defined in the `xkb_keycodes` section. 435 436Symbols are named using the symbolic names from the 437`xkbcommon/xkbcommon-keysyms.h` file. A group of symbols is enclosed in brackets 438and separated by commas. Each element of the symbol arrays corresponds to a 439different modifier level. In this example, the symbol (keysym) `XKB_KEY_q` for 440level 1 and `XKB_KEY_Q` for level 2. 441 442#### Groups 443 444Each group represents a list of symbols mapped to a keycode: 445 446 name[Group1]= "US/ASCII"; 447 name[Group2]= "Russian"; 448 ... 449 key <AD01> { [ q, Q ], 450 [ Cyrillic_shorti, Cyrillic_SHORTI ] }; 451 452A long-form syntax can also be used: 453 454 key <AD01> { 455 symbols[Group1]= [ q, Q ], 456 symbols[Group2]= [ Cyrillic_shorti, Cyrillic_SHORTI ] 457 }; 458 459Groups can also be omitted, but the brackets must be present. The following 460statement only defines the Group3 of a mapping: 461 462 key <AD01> { [], [], [ q, Q ] }; 463 464## Virtual modifier statements 465 466Statements of the form: 467 468 virtual_modifiers LControl; 469 470Can appear in the `xkb_types`, `xkb_compat`, `xkb_symbols` sections. 471 472TODO 473