How to add a new keyboard layout (Custom keyboard layout definition)

Creating custom keyboard layout

For example, I'm looking to add a new modified English (international AltGr dead keys) US layout with swapped r,R & t,T. I will name it XY swapped:RT English (international AltGr dead keys).

  1. Create new symbols file:

    sudo nano /usr/share/X11/xkb/symbols/xy

     default  partial alphanumeric_keys
     xkb_symbols "basic" {
    
        name[Group1]= "XY swapped:RT English (international AltGr dead keys)";
    
        include "us(altgr-intl)"
    
     // my custom changes:
    
        key <AD04> { [        t, T,           ediaeresis,   Ediaeresis      ] };
        key <AD05> { [        r, R,           thorn,        THORN           ] };
    
    
        include "level3(ralt_switch)"
     };
    

    include "us(altgr-intl)" means it inherits all key from that layout only key overridden here.

    You may use Keyboard Layout Editor which is a GUI program that helps create or edit XKB keyboard layouts. Thanks to @Glutanimate

  2. Add the new layout declaration to /usr/share/X11/xkb/rules/evdev.xml (copy & modify us layout section)

    Reference: Make new variant keyboard layout available in Settings?

    sudo gedit /usr/share/X11/xkb/rules/evdev.xml

     ...
     <layout>
       <configItem>
         <name>xy</name>
    
         <shortDescription>en</shortDescription>
         <description>XY swapped:RT English (international AltGr dead keys)</description>
         <languageList>
                  <iso639Id>eng</iso639Id>
                  <iso639Id>fra</iso639Id>
                  <iso639Id>ger</iso639Id>
         </languageList>
       </configItem>
       <variantList>
       </variantList>
     </layout>
     ...
    
  3. Delete xkb cache.

     sudo rm /var/lib/xkb/*.xkm
    

    Notes:

    To load changes to the layouts menu simply restart gnome-keyboard-preferences; relaunching the window manager should not be necessary.

    There is a change in Ubuntu version 13.10 which causes the keyboard settings cache to not refresh after files in "/usr/share/X11/xkb/symbols" 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".

    Reference: Howto: Custom keyboard layout definitions

    or

     sudo dpkg-reconfigure xkb-data
    

    Reference: Why did 13.10 break my custom keyboard layout?

    Custom keyboard layout

Reply to fix OP modifications

  • (Update #1 wrong parent layout) You have modified xkb_symbols "pes_part_basic" which is in irwinxp file: that's ok.

    but xkb_symbols "pes" in irwinxp file still:

      include "ir(pes_part_basic)"
    

    which should be:

      include "irwinxp(pes_part_basic)"
    
  • (Update #2 custom icon) If <shortDescription>en</shortDescription> changed to <shortDescription>xy</shortDescription>. So the new layout will distinct from En and it will appear as Xy.

    Most probably there will be no icon for it in ubuntu-mono. Then the indicator will dynamically generate new one which may not follow current theme.

    1. Copy any layout icons for dark/light mono themes:

       sudo cp /usr/share/icons/ubuntu-mono-dark/status/22/indicator-keyboard-En.svg /usr/share/icons/ubuntu-mono-dark/status/22/indicator-keyboard-Xy.svg
       sudo cp /usr/share/icons/ubuntu-mono-light/status/22/indicator-keyboard-En.svg /usr/share/icons/ubuntu-mono-light/status/22/indicator-keyboard-Xy.svg
      
    2. SVG files are just XML. Open then for editing and change text value from En to Xy:

       sudo nano /usr/share/icons/ubuntu-mono-dark/status/22/indicator-keyboard-Xy.svg
       sudo nano /usr/share/icons/ubuntu-mono-light/status/22/indicator-keyboard-Xy.svg
      

      Example:

       <?xml version="1.0" encoding="UTF-8" standalone="no"?>
       <svg width="22" xmlns="http://www.w3.org/2000/svg" version="1.1" height="22">
        <defs>
         <mask id="m">
          <rect y="0" x="0" style="fill:#fff" height="22" width="22"/>
          <text y="15.5" x="5" style="font-size:12;font-family:Ubuntu;font-weight:500;fill:black">Xy</text>
         </mask>
        </defs>
        <rect style="fill:#dfdbd2" mask="url(#m)" rx="2" height="20" width="20" y="1" x="1"/>
       </svg>
      
    3. Update theme cache:

       sudo update-icon-caches /usr/share/icons/ubuntu-mono-*/            
      
    4. Logout/login

      new layout with custom icon

Other Helpful References

  • How to modify a keyboard layout in Linux (thanks to @Rmano)
  • Building an XKB Keyboard (thanks to @Glutanimate)