🏠

Custom Sway keyboard mapping

Published on 2025-03-28

I thought that I had a problem: I’m an avid user of the EurKEYS keyboard layout and I wanted to type a character I (thought) I didn’t had available: §. Even though it turned out it actually is available (AltGr+Shift+s), it was a fun rabbit hole and I feel like it’s worth sharing, in case anyone wants to tweak the keyboard layout.

When you press a key on your keyboard, it sends a scancode to your computer. This scancode is a unique number that corresponds to that specific key. To find the scan code for a key, you can use e.g. extra/wev, and see that (on my machine) a has the scan code 38, s 39, and so on:

[14:     wl_keyboard] key: serial: 183354; time: 65458672; key: 38; state: 1 (pressed)
                      sym: a            (97), utf8: ’a’

[14:     wl_keyboard] key: serial: 183667; time: 65512523; key: 39; state: 1 (pressed)
                      sym: s            (115), utf8: ’s’

There are many layouts, and their scancodes differ. wayland-book has more details, if you are interested in a deep-dive on that topic.

This scancode has to be mapped to a specific character. There are different ways how this mapping is done, the most common way is to use xkb, especially since this is shipped by default in many distros. In general, wayland is able to support different input methods, but the only defined one is xkb.

The X in XKB

“There is something odd though: XKB stands for the X keyboard extension, but Sway is on Wayland. Sway uses wlroots for Wayland building blocks. This further uses libxkbcommon to parse and handle keyboard layouts. This library implements the XKB specification and does not have any relation with the X server itself. The symbols in /usr/share/X11/xkb are distributed in the xkeyboard-config package.”

© Sameer Puri, licensed under CC BY-SA 4.0

Step 1: Generate custom XKB mapping

Let’s create ~/.xkb/symbols/eurokeys-custom with the following content:

default partial alphanumeric_keys
xkb_symbols "basic" {
	include "eu"
	name[Group1] = "Custom eurokey adaption";
	key <AD10> { [ p, P, section] };
};

Step 2: Use new XKB mapping

Depending on your desktop enviroment, set the new keyboard layout. In Sway, you can do this by setting the following code in .config/sway/config:

input type:keyboard {
    xkb_layout eurokeys-custom
}