Skip to content

Instantly share code, notes, and snippets.

@antongulenko
Last active January 15, 2021 12:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antongulenko/9e4b4251e77b34e281caaf4cf3f63bcf to your computer and use it in GitHub Desktop.
Save antongulenko/9e4b4251e77b34e281caaf4cf3f63bcf to your computer and use it in GitHub Desktop.
X11: Implement CAPS+LEFT/RIGHT shortcuts for Home/End buttons

X11: Caps-lock modifier for Home/End through arrow keys

We will use XKB to add a custom keyboard layout that turns the caps-lock key into a modifier button, that turns the left/right arrow keys into Home/End buttons.

Tested on Ubuntu 20.04, but should work for any X11 system with XKB.

The motiviation for this is the keyboard laoyout on the Dell XPS 13 9300, where the Home/End keys have been moved to the top right corner (on F11/F12). Repurposing the caps-lock key makes this easier to use.

WARNING these changes affect the keyboard settings at the very core of the X system, and can potentially break the graphical boot of your system. To undo these changes, execute the following:

sudo apt install --reinstall xkb-data

If your graphical boot fails, you can log in through a tty (Ctrl+Alt+F2), execute the above command, and reboot.

Base directory for all steps is: /usr/share/X11/xkb

Step 1: Add a key type for the new modifier

Create file types/mod2level2 with content:

partial xkb_types "mod2level2" {

    type "MOD2_LEVEL2" {
        modifiers= Mod2;
        map[Mod2]= Level2;
        level_name[Level1]= "Base";
        level_name[Level2]= "CapsLockButton";
    };

};

This will allow us to register keys with alternative functions based on the Mod2 modifier, which we will bind the CAPS key to. Unfortunately, there is no default key type that we can use for this.

Step 2: Register our new key type

Edit file types/complete, insert following line in the main block:

include "mod2level2"

Step 3: Create custom key symbol map

Find the keyboard layout file that you are using in the symbols folder. For me, it's symbols/us. Inside that file, find your variant. For me it's altgr-intl. Add the following snippet at the end of that file (here, symbols/us):

partial alphanumeric_keys
xkb_symbols "caps-home-end" {

    // Disable the regular functionality of the caps-lock button
    key <CAPS> {         [           VoidSymbol  ] };

    // Bind the left/right keys to our new key-type and set the secondary functionality to Home/End
    key <LEFT> {
        type= "MOD2_LEVEL2",
        symbols[Group1]= [            Left,        Home ]
    };
    key <RGHT> {
        type= "MOD2_LEVEL2",
        symbols[Group1]= [            Right,       End ]
    };
    
    // Inherit the rest of the layout from another variant
    include "us(altgr-intl)"

    // Bind the <CAPS> key to our modifier
    // Mod2 must match the modifier in groups/mod2level2
    modifier_map Mod2 { <CAPS> };

};

The name caps-home-end is arbitrary, but should not collide with any other existing variant. Replace the include statement with the base-variant you are using.

Step 4: Register our new variant

Edit file rules/evdev.xml. Insert the following snippet in the section <layoutList> -> <layout> (<name>"us"</name>) -> <variantList>

The caps-home-end name must match the name from Step 3. The languageList is probably optional, but should match whatever is in your base variant (for me, altgr-intl). The description field will be the name displayed in the settings UI.

<variant>
  <configItem>
    <name>caps-home-end</name>
    <description>US/intl/altgr, Home/End on CAPS+left/right</description>
    <languageList>
      <iso639Id>eng</iso639Id>
      <iso639Id>fra</iso639Id>
      <iso639Id>deu</iso639Id>
    </languageList>
  </configItem>
</variant>

Step 5: Activate and use new keyboard layout

Execute: sudo dpkg-reconfigure xkb-data

Reboot (probably logging out is enough)

Select new layout in Settings -> Region & Language -> Input Sources

For me, I first select English (United States), then scroll to the bottom of the list of variants and select US/intl/altgr, Home/End on CAPS+left/right.

I you make any changes to any of the files afterwards, you should repeat ALL parts of Step 5, including selecting a different layout, and then switching back to the new layout.

Step 6: Get used to the new combination

Not perfect, but better than using Fn+F11 and Fn+F12 for Home/End... Seriously, Dell...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment