iTranslated by AI
Remapping keys at the kernel level with keyd
Introduction
When Pop!_OS 24 LTS was released, I learned that their original desktop environment, COSMIC, was no longer in beta.
I tried Pop!_OS on a VM to see what it was like and found it to be very user-friendly. Since I was already thinking about switching from i3wm on Ubuntu to the Wayland-compatible sway, I decided to migrate my entire OS.
History of Migration to Pop!_OS / COSMIC and Key Bindings
As a window manager, COSMIC allows you to choose between tiling and floating modes.
It doesn't force you to pick one or the other; it has the flexibility to automatically tile windows by default while still allowing you to set specific windows to float.
While i3 and sway share backward compatibility, COSMIC is an entirely different environment. You have to re-learn some of the shortcuts used in i3, but they aren't significantly different. Since the basic spec of combining the super key with other keys (e.g., super+j to move to the left window) remains the same, I was able to get used to it quickly.
However, in i3, I assigned capslock to the super key by specifying it within i3/config. Since that setting was only for the i3 environment, I wanted to apply the setting system-wide, which led me to keyd.
What is keyd?
keyd is described as:
Linux lacks a good key remapping solution. In order to achieve satisfactory results a medley of tools need to be employed (e.g xcape, xmodmap) with the end result often being tethered to a specified environment (X11). keyd attempts to solve this problem by providing a flexible system wide daemon which remaps keys using kernel level input primitives (evdev, uinput).
As the description suggests, it performs key map editing at the kernel level, so the settings are reflected across the entire system.
In other words, traditional Linux required multiple tools for key remapping and was often constrained by X11 dependence. Because keyd operates at the kernel level, settings are applied system-wide regardless of the desktop environment.
From Build to Configuration
Build the latest version from source:
git clone https://github.com/rvaiya/keyd
cd keyd && make && sudo make install
sudo systemctl enable keyd --now
Create a configuration file:
sudo mkdir -p /etc/keyd
sudo vim /etc/keyd/default.conf
Write the desired changes:
[ids]
# Apply to all keyboards
*
[main]
# Escape on tap, Super on hold
capslock = overload(meta, esc)
Tap or Hold as You Please
A unique feature here is the ability to assign two different roles based on how long you hold the same key.
In the configuration above, overload(meta, esc) is used, so a short press triggers esc, while holding it down acts as the super key.
When using the super key for COSMIC shortcuts, I mostly hold it down, but when using vim, I can tap it quickly to use it as esc (my hands are small, so the original esc key is hard to reach).
Once you have written and saved the necessary settings:
# Reload settings
sudo keyd reload
# Check status
sudo systemctl status keyd
It should be reflected. You can also verify that it is working as assigned by pressing the keys:
# Monitor key input
sudo keyd monitor
More Advanced Settings
Although I haven't done much advanced configuration yet, as mentioned in the official documentation, you can also assign keys per application:
[alacritty]
alt.] = macro(C-g n)
alt.[ = macro(C-g p)
[chromium]
alt.[ = C-S-tab
alt.] = macro(C-tab)
Since COSMIC has plenty of shortcuts if you just assign the super key, I don't feel the need to configure much else, but you could think of various applications such as vimium-like usage.
Clear and Easy-to-use Key Settings
There are various ingenious ideas here as well.
For Japanese input, I use fcitx5-configtool to set the 'Muhenkan' (non-conversion) key to turn Japanese input ON and 'Henkan' (conversion) to turn it OFF. It's true that the keys on either side of the space bar are easily accessible, so assigning other functions to them is also an interesting idea.
I wish you all a happy keyd life!!
Discussion