Mapping key bindings

key mappingkeyboardx11

Background: I got an Acer Chromebook yesterday and I am running Ubuntu on it (I am also somewhat new to Linux). The keyboard does not have dedicated Home and End buttons since in ChromeOS there are replacement key bindings (that do not work under the running Linux).

My question: is if there is a way to assign certain key binding in Linux to Home and End key actions so that I can recover those two keys by key combinations.

Best Answer

In xkeyboard-config-1.7-nav-keys.patch is explained how it used to be done, with XKB. More recent versions of Chrome OS now use Ash.

Create directories called ~/.xkb/keymap, ~/.xkb/types and ~/.xkb/symbols. Create a file ~/.xkb/types/chromebook containing the following definition:

xkb_types "chromebook" {
    virtual_modifiers Alt;
    type "ARROW" {
        modifiers              = Shift+Lock+Alt+Control;
        map[Shift]             = Level2;
        map[Lock]              = Level2;
        map[Alt]               = Level3;
        map[Alt+Shift]         = Level3;
        map[Alt+Lock]          = Level3;
        map[Control+Alt]       = Level4;
        map[Control+Alt+Shift] = Level4;
        map[Control+Alt+Lock]  = Level4;
        level_name[Level1]     = "Base";
        level_name[Level2]     = "Caps";
        level_name[Level3]     = "Alt";
        level_name[Level4]     = "ControlAlt";
    };
}

Create a file ~/.xkb/symbols/chromebook containing the following definition:

xkb_symbols "chromebook" {
    key <UP> {
        type="ARROW",
        repeat=yes,
        [ Up, Up, Prior, Home ],
        actions[Group1] = [
            NoAction(),
            NoAction(),
            RedirectKey(key=<PGUP>, clearmods=Alt),
            RedirectKey(key=<HOME>, clearmods=Control+Alt)
        ]
    };
    key <DOWN> {
        type="ARROW",
        repeat=yes,
        [ Down, Down, Next, End ],
        actions[Group1] = [
            NoAction(),
            NoAction(),
            RedirectKey(key=<PGDN>, clearmods=Alt),
            RedirectKey(key=<END>, clearmods=Control+Alt)
        ]
    };
};

Run the command setxkbmap -print >~/.xkb/keymap/chromebook. Edit the resulting file and add +chromebook inside the include directives for xkb_types and xkb_symbols; the file should look like this:

xkb_keymap {
        xkb_keycodes  { include "evdev+aliases(qwerty)" };
        xkb_types     { include "complete+chromebook"      };
        xkb_compat    { include "complete"      };
        xkb_symbols   { include "pc+us+inet(evdev)+chromebook"     };
        xkb_geometry  { include "pc(pc105)"     };
};

Add the following command to the startup applications:

xkbcomp -I ~/.xkb -R ~/.xkb keymap/chromebook $DISPLAY

Note: this is untested, I'm not very familiar with XKB and it's poorly documented. I tried to follow Vincent Lefèvre's XKB tips.

Related Question