XKB With Sway

How to set up multiple keyboards with the Sway Wayland compositor.

This explains how to configure multiple keyboards in Sway, and how to use advanced configuration when regular xkb_options are not enough.

Sway configuration

Luckily, Sway has great support for multiple keyboards with different layouts, so it nicely adjusts to whatever keyboard you have plugged in or can use separate configurations for a physical keyboard and the laptop keyboard.

First, identify the identifiers for the keyboards you have plugged in, using the following command:

$ swaymsg -t get_inputs

Look for the lines starting with Identifier: for the device you’re interested in.

Next, add one section for each of your keyboards to ~/.config/sway/config:

input "1278:32:PFU_Limited_HHKB-Classic" {
	xkb_layout "us"
	xkb_model "hhk"
	xkb_options "compose:ralt"
	xkb_capslock "disabled"

input "1133:49948:Logitech_USB_Keyboard" {
	xkb_file ".xkb/keymap/logitech"

The example above lists a HHKB (“Happy Hacking”) keyboard where right Alt acts as the Compose key. More options are documented in the man pages sway-input(5) as well as xkeyboard-config(7).

In the case where the existing XKB options are not sufficient, you need to refer to a separate XKB configuration file, as described below.

XKB configuration

To configure my Logitech keyboard, the plan was to have the key mapping reasonably close to the HHKB layout.

In my case, I have two files under ~/.xkb (the directories need to first be created): This is the ~/.xkb/keymap/logitech file:

// Derived from `setxkbmap -print`,
// added the "+unixy" part to xkb_symbols.
xkb_keymap {
	xkb_keycodes  { include "evdev+aliases(qwerty)"	};
	xkb_types     { include "complete"	};
	xkb_compat    { include "complete"	};
	xkb_symbols   { include "pc+us+inet(evdev)+unixy"	};
	xkb_geometry  { include "pc(pc105)"	};

This file is the output from running setxkbmap -print, but adds the +unixy part to the xkb_symbols section. This refers to the ~/.xkb/symbols/unixy file with the following content:

partial alphanumeric_keys
xkb_symbols "unixy" {
	// exchange backspace and backslash
	key <BKSL> {	[ BackSpace, Backspace ] };
	key <BKSP> {	[ backslash, bar ] };

	// caps is ctrl
	key <CAPS> {	[ Control_L ] };
	modifier_map Control { <CAPS> };

	// right alt is compose
	key <RALT> {	[ Multi_key ] };