2024-09-04

custom keyboard layout under linux X11

only requires a file like /usr/share/X11/xkb/symbols/mylayout. you can look at other files in the directory for examples, particularly for the character identifiers to use. layouts can be applied with "setxkbmap mylayout", manually from a terminal, from .xinitrc or alternatively with a configuration file under /etc/X11/xorg.conf.d.

contents could look like the following, where the "key <AE01>" prefixes correspond to keys in rows on the keyboard:

default
partial alphanumeric_keys modifier_keys
xkb_symbols "mylayout" {
    name[Group1]= "myshortdescription";
    key <TLDE> { [ asciitilde, dead_diaeresis, asciitilde ] };
    key <AE01> { [ 1, exclam, backtick ] };
    key <AE02> { [ 2, at ] };
    key <AE03> { [ 3, numbersign ] };
    key <AE04> { [ 4, dollar ] };
    key <AE05> { [ 5, percent ] };
    key <AE06> { [ 6, asciicircum ] };
    key <AE07> { [ 7, ampersand ] };
    key <AE08> { [ 8, asterisk ] };
    key <AE09> { [ 9, less ] };
    key <AE10> { [ 0, greater ] };
    key <AE11> { [ braceleft, bracketleft ] };
    key <AE12> { [ braceright, bracketright ] };
    key <AD01> { [ q, Q ] };
    key <AD02> { [ g, G ] };
    key <AD03> { [ m, M ] };
    key <AD04> { [ l, L ] };
    key <AD05> { [ w, W ] };
    key <AD06> { [ b, B ] };
    key <AD07> { [ y, Y ] };
    key <AD08> { [ u, U ] };
    key <AD09> { [ v, V ] };
    key <AD10> { [ colon, semicolon ] };
    key <AD11> { [ question, apostrophe ] };
    key <AD12> { [ equal, plus ] };
    key <BKSL> { [ bar, parenright ] };
    key <AC01> { [ d, D ] };
    key <AC02> { [ s, S ] };
    key <AC03> { [ t, T ] };
    key <AC04> { [ n, N ] };
    key <AC05> { [ r, R ] };
    key <AC06> { [ i, I ] };
    key <AC07> { [ a, A ] };
    key <AC08> { [ e, E ] };
    key <AC09> { [ o, O ] };
    key <AC10> { [ h, H ] };
    key <AC11> { [ slash, backslash ] };
    key <AB01> { [ z, Z ] };
    key <AB02> { [ x, X ] };
    key <AB03> { [ c, C ] };
    key <AB04> { [ f, F ] };
    key <AB05> { [ j, J ] };
    key <AB06> { [ k, K ] };
    key <AB07> { [ p, P ] };
    key <AB08> { [ comma, quotedbl ] };
    key <AB09> { [ period, parenleft ] };
    key <AB10> { [ minus, underscore ] };
};

custom keyboard layouts and fcitx

the language input method system fcitx reloads detected keyboard layouts when switching input methods. it does not detect customized x-server keyboard layouts unless they are registered in additional files. replace "mylayout" with the name of the keyboard layout you want to add.

add to /usr/share/X11/xkb/rules/evdev.xml inside <layoutList>

<layoutList>
  <layout>
    <configItem>
      <name>mylayout</name>
      <shortDescription>myshortdescription</shortDescription>
      <description>mydescription</description>
      <languageList>
        <iso639Id>eng</iso639Id>
      </languageList>
    </configItem>
  </layout>

add to /usr/share/X11/xkb/rules/evdev.lst after "! layout"

! layout
  mylayout          myshortdescription

fcitx may need to be restarted for changes to take effect