Nemirwen's corner

Computer graphics, linux, and more from the cyberwitch's hut

Custom fr-oss xkb layout for ANSI keyboards


Background

There is an unfortunate reality that many of us europeans discover when shopping for a new cool keyboard: the US american ANSI disposition dominates the market with its measly 104 keys. Meanwhile the 105 keys ISO disposition is not quite as available. Problem is, the main french keyboard layouts (fr, fr-latin*, fr-oss) use that 105th key to host the < and > chars, which gets limiting really fast when doing anything technical.

A comparison of two physical (mechanical) computer keyboard layout standards: ANSI (common in North America and Asia) and ISO (common in Europe and Latin America). Keys that are not present in both standards are highlighted in pink. Keys that may not be present on all models are shown with interrupted outlines.
        Note that this image shows physical layouts as they most often appear and may not fully represent said standards. Note that the alphanumeric labels are only for general orientation and do not represent the visual layout of any country/language.
The missing key is the red one on the bottom left on the ISO keyboard.
Illustration by Brilliantwiki2, licensed under CC BY-SA 4.0

In order to solve this issue (and keep using an azerty layout), I'm going to create a custom xkb variant, based an fr-oss, which adds < and > on altgr+w and altgr+x respectively.

The new variant

First, it can be helpful to look at /usr/share/X11/xkb/symbols/fr to get an idea of the syntax and layout of an xkb symbols file, for example:

[...]

partial alphanumeric_keys
xkb_symbols "oss" {

    include "level3(ralt_switch)"
    include "nbsp(level4n)"
    include "keypad(oss)"

    name[Group1]="French (alt.)";

    // First row
    key <TLDE>  { [      twosuperior,    threesuperior,          onesuperior,          dead_cedilla ] }; // ² ³ ¹ ¸
    key <AE01>  { [        ampersand,                1,           dead_caron,           dead_ogonek ] }; // & 1 ˇ ˛

    [...]

    key <AB10>  { [           exclam,          section,           exclamdown,             0x1002212 ] }; // ! § ¡ −
};

[...]

So, let's write our own in ~/.config/xkb/symbols/custom (we'll get to why we chose that particular name later):

partial alphanumeric_keys
xkb_symbols "fr_oss_ansi" {
    // Brings < and > to ANSI keyboards

    include "fr(oss)"

    name[Group1]="French (alt., ansi)";

    key <AB01>  { [w, W,    less,  guillemotleft ] }; // w W < «
    key <AB02>  { [x, X, greater, guillemotright ] }; // x X > »
};

By declaring the variant partial, we are able to reuse everything that was defined in the fr-oss variant.

Also, each element in [w, W, less, guillemotleft ] correspond to a different modifier group, respectively: none, shift, altgr, shift+altgr.

Using the new variant

Until recently, customizing xkb was just a big mess of playing with system files. Luckily, it seems that three years ago, things got a bit better as xkeyboard-config, the repo for the X Keyboard Extension, accepted a merge request to add a custom layout to their config. But crucially, they didn't provide a definition for it, in order to allow users to add their very own layout, with as many variants as needed.

So, on a single user system, we can simply link our config to the system xkb path:

sudo ln -s \
    ~/.config/xkb/symbols/custom \
    /usr/share/X11/xkb/symbols/custom

Another advantage of doing things this way is that some wayland compositors, for example sway, support simply reading your xkb configuration from ~/.config/xkb/.

Now, if you want to test things out, you can just use xkbcomp to test for syntax errors before loading the new layout with setxkbmap:

xkbcomp ~/.config/xkb/symbols/custom && \
    setxkbmap -layout custom -variant fr_oss_ansi

If successfull, altgr+w should give you back a nice <!

From there, if you're reading this, you should all be grown ups able to set a persistent configuration, either in /etc/X11/xorg.conf.d/ or in ~/.config/sway/config.

Cheers! =)