Introduction
In this article, we will discuss how to remap keyboard keys for Linux Apple keyboards with Touch Bar on Arch Linux with Wayland and KDE Plasma. This guide will help you if you are using Asahi Linux, a work-in-progress Linux distribution for Apple Silicon machines like the M1 Mac. Specifically, we will cover modifier keys and provide a detailed context of the topic with subtitles, paragraphs, code blocks, and references.
Prerequisites
Before diving into the process of remapping keyboard keys, ensure you have the following installed on your Arch Linux with Wayland and KDE Plasma:
- Sway or any other Wayland compositor
- systemd
- xkbcomp
To install these packages, use the following command:
sudo pacman -S sway systemd xkbcomp
Understanding Keyboard Mapping and XKB
Keyboard mapping is the process of assigning specific keys on a keyboard to specific functions or symbols. XKB (X Keyboard Extension) is a keyboard mapping and configuration system for the X Window System and Wayland compositors like Sway.
XKB Components
An XKB configuration consists of the following components:
- Keyboard layout
- Keyboard model
- Keyboard variants
- Keyboard options
Remapping Modifier Keys
To remap modifier keys, like the Caps Lock key or the Touch Bar keys, we need to modify the XKB configuration. In our example, we will remap the Caps Lock key to Escape and use the Touch Bar keys as additional modifier keys.
Creating an XKB Configuration Snippet
Create a new file called mykeyboard with the following content. Replace the device ID (vendor:product:variant) with your Apple Touch Bar keyboard's correct device ID:
partial alphanumeric_keys
xkb_keycodes {
include "evdev+aliases(qwerty)"
};
xkb_types {
type "MY_CUSTOM_LEVEL1" {
modifiers = Shift+LevelThree;
level_name[Level1] = "Base";
level_name[Level2] = "Shift";
level_name[Level3] = "Caps_Lock";
};
};
xkb_symbols {
include "pc+us+inet(evdev)"
key {
type = "MY_CUSTOM_LEVEL1",
symbols[Level1] = { Escape };
};
include "apple(apple_keyboard_t1)"
};
xkb_geometry {
include "pc(pc105)"
};
The configuration snippet above:
- Includes the standard QWERTY keycode aliases
- Creates a custom type for Caps_Lock, mapping it to Escape
- Includes the standard US keyboard layout and symbols
- Includes the Apple Touch Bar keyboard layout
Loading the XKB Configuration Snippet
After creating the XKB configuration snippet, load it during system startup by creating a new service unit for systemd. Create a new file called mykeyboard.service with the following content:
[Unit]
Description=Load My Custom Keyboard Layout
[Service]
Type=simple
ExecStart=/bin/bash -c "xkbcomp -I$XDG_DATA_DIR/xkb - $DISPLAY <(xkbcomp -i $XDG_DATA_DIR/xkb/ | sed -e 's/!\rust-impl/mykeyboard/')"
[Install]
WantedBy=multi-user.target
Load and enable the new service unit:
sudo systemctl daemon-reload
sudo systemctl enable mykeyboard.service