一个 Linux 下的按键映射守护进程
A key remapping daemon for Linux

原始链接: https://github.com/rvaiya/keyd

**keyd** 是一款适用于 Linux 的轻量级系统级按键重映射守护进程,它克服了 xmodmap 等依赖显示服务器工具的局限性。它使用 C 语言编写,利用内核级输入原语(evdev/uinput),提供了低延迟、与环境无关的重映射功能,适用于 X11、Wayland 和虚拟终端。 主要功能包括: * **高级重载:** 复杂的点击/按住行为(例如:按住 Caps Lock 时作为 Control 键,点击时作为 Escape 键)。 * **层(Layers):** 支持复杂的按键层,类似于 QMK 等自定义固件。 * **单次修饰键(One-shot Modifiers):** 启用“粘滞”修饰键,使打字更轻松。 * **灵活性:** 支持 Unicode、特定键盘配置以及特定应用程序的重映射。 与基于 Haskell 的替代品(如 KMonad)不同,`keyd` 优先考虑极简设计,将代码库保持在 2000 行 C 代码以内,以实现极致的性能和简洁性。它使用直观的系统级配置格式(`/etc/keyd/default.conf`),并提供了一种防故障按键组合(Backspace+Escape+Enter),以防止因错误配置导致系统锁死。`keyd` 可通过大多数主流包管理器获取,对于那些寻求以 Unix 哲学定制键盘、且无需刷写自定义固件的用户来说,它是理想的选择。

```Hacker News最新 | 过往 | 评论 | 提问 | 展示 | 招聘 | 提交登录一个用于 Linux 的按键重映射守护程序 (github.com/rvaiya)7 分,作者:joooscha,1 小时前 | 隐藏 | 过往 | 收藏 | 2 条评论帮助 garciansmith 5 分钟前 | 下一条 [–] 我发现这类键盘重映射功能在笔记本电脑上非常必要,因为我已经习惯了在桌面上使用支持 QMK 固件的外接键盘(尽管我最近在用 Kmonad)。回复joooscha 1 小时前 | 上一条 [–] 我用它将空格键重映射为修饰键,觉得这可能对某些人有用。回复 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 加入 YC | 联系 搜索: ```
相关文章

原文

Kofi

Packaging status

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).

The config format has undergone several iterations since the first release. For those migrating their configs from v1 it is best to reread the man page (man keyd).

See also: changelog.

  • Speed (a hand tuned input loop written in C that takes <<1ms)
  • Simplicity (a config format that is intuitive)
  • Consistency (modifiers that play nicely with layers by default)
  • Modularity (a UNIXy core extensible through the use of an IPC mechanism)

keyd has several unique features many of which are traditionally only found in custom keyboard firmware like QMK as well as some which are unique to keyd.

Some of the more interesting ones include:

  • Layers (with support for hybrid modifiers).
  • Key overloading (different behaviour on tap/hold).
  • Keyboard specific configuration.
  • Instantaneous remapping (no more flashing :)).
  • A client-server model that facilitates scripting and display server agnostic application remapping. (Currently ships with support for X, sway, and gnome (wayland)).
  • System wide config (works in a VT).
  • First class support for modifier overloading.
  • Unicode support.
  • Would like to experiment with custom layers (i.e custom shift keys) and oneshot modifiers.
  • Want to have multiple keyboards with different layouts on the same machine.
  • Want to be able to remap C-1 without breaking modifier semantics.
  • Want a keyboard config format which is easy to grok.
  • Like tiny daemons that adhere to the Unix philosophy.
  • Want to put the control and escape keys where God intended.
  • Wish to be able to switch to a VT to debug something without breaking their keymap.
  • A tool for programming individual key up/down events.
  • Your favourite C compiler
  • Linux kernel headers (already present on most systems)
  • python (for application specific remapping)
  • python-xlib (only for X support)
  • dbus-python (only for KDE support)

Note: master serves as the development branch, things may occasionally break between releases. Releases are tagged, and should be considered stable.

git clone https://github.com/rvaiya/keyd
cd keyd
make && sudo make install
sudo systemctl enable --now keyd
  1. Install and start keyd (e.g sudo systemctl enable keyd --now)

  2. Put the following in /etc/keyd/default.conf:

[ids]

*

[main]

# Maps capslock to escape when pressed and control when held.
capslock = overload(control, esc)

# Remaps the escape key to capslock
esc = capslock

Key names can be obtained by using the keyd monitor command. Note that while keyd is running, the output of this command will correspond to keyd's output. The original input events can be seen by first stopping keyd and then running the command. See the man page for more details.

  1. Run sudo keyd reload to reload the config set.

  2. See the man page (man keyd) for a more comprehensive description.

Config errors will appear in the log output and can be accessed in the usual way using your system's service manager (e.g sudo journalctl -eu keyd).

Note: It is possible to render your machine unusable with a bad config file. Should you find yourself in this position, the special key sequence backspace+escape+enter should cause keyd to terminate.

Some mice (e.g the Logitech MX Master) are capable of emitting keys and are consequently matched by the wildcard id. It may be necessary to explicitly blacklist these.

Application Specific Remapping (experimental)

E.G

[alacritty]

alt.] = macro(C-g n)
alt.[ = macro(C-g p)

[chromium]

alt.[ = C-S-tab
alt.] = macro(C-tab)
  • Run:

    keyd-application-mapper

You will probably want to put keyd-application-mapper -d somewhere in your display server initialization logic (e.g ~/.xinitrc) unless you are running Gnome.

See the man page for more details.

Experimental support for single board computers (SBCs) via usb-gadget has been added courtesy of Giorgi Chavchanidze.

See usb-gadget.md for details.

Third-party packages exist for some distributions. If you wish to add yours to the list please file a PR. These are kindly maintained by community members, no personal responsibility is taken for them.

keyd package maintained by @jirutka.

Arch Linux package maintained by Arch packagers.

A keyd package is available in Debian 13 ("trixie") and later. To install:

COPR package maintained by @alternateved.

GURU package maintained by [email protected].

opensuse package maintained by @bubbleguuum.

Easy install with sudo zypper in keyd.

A keyd package is available in Ubuntu 25.04 ("plucky") and later. To install:

In addition, the latest Debian package backported to various Ubuntu releases can be found in the ppa:keyd-team/ppa archive.

xbps package maintained by @Barbaross.

Easy install with sudo xbps-install -Su keyd.

[ids]

*

[main]

leftshift = oneshot(shift)
capslock = overload(symbols, esc)

[symbols]

d = ~
f = /
...

This overrides specific alt combinations macOS users might be more familiar with, while keeping the rest intact.

[ids]
*

[alt]

x = C-x
c = C-c
v = C-v

a = C-a
f = C-f
r = C-r
z = C-z

Many users will probably not be interested in taking full advantage of keyd. For those who seek simple quality of life improvements I can recommend the following config:

[ids]

*

[main]

shift = oneshot(shift)
meta = oneshot(meta)
control = oneshot(control)

leftalt = oneshot(alt)
rightalt = oneshot(altgr)

capslock = overload(control, esc)
insert = S-insert

This overloads the capslock key to function as both escape (when tapped) and control (when held) and remaps all modifiers to 'oneshot' keys. Thus to produce the letter A you can now simply tap shift and then a instead of having to hold it. Finally it remaps insert to S-insert (paste on X11).

Why is my trackpad is interfering with input after enabling keyd?

libinput, a higher level input component used by most wayland and X11 setups, includes a feature called 'disable-while-typing' that disables the trackpad when typing.

In order to achieve this, it needs to distinguish between internal and external keyboards, which it does by hard coding a rules for specific hardware ('quirks'). Since keyd creates a virtual device which subsumes both external and integrated keyboards, you will need to instruct libinput to regard the keyd virtual device as internal.

This can be achieved by adding the following to /etc/libinput/local-overrides.quirks (which may need to be created):

[Serial Keyboards]

MatchUdevType=keyboard
MatchName=keyd*keyboard
AttrKeyboardIntegration=internal

Credit to @mark-herbert42 and @canadaduane for the original solution.

What about xmodmap/setxkbmap/*?

xmodmap and friends are display server level tools with limited functionality. keyd is a system level solution which implements advanced features like layering and oneshot modifiers. While some X tools offer similar functionality I am not aware of anything that is as flexible as keyd.

keyd was written several years ago to allow me to easily experiment with different layouts on my growing keyboard collection. At the time kmonad did not exist and custom keyboard firmware like QMK (which inspired keyd) was the only way to get comparable features. I became aware of kmonad after having published keyd. While kmonad is a fine project with similar goals, it takes a different approach and has a different design philosophy.

Notably keyd was written entirely in C with performance and simplicitly in mind and will likely never be as configurable as kmonad (which is extensible in Haskell). Having said that, it supplies (in the author's opinion) the most valuable features in less than 2000 lines of C while providing a simple language agnostic config format.

Why doesn't keyd implement feature X?

If you feel something is missing or find a bug you are welcome to file an issue on github. keyd has a minimalist (but sane) design philosophy which intentionally omits certain features (e.g execing arbitrary executables as root). Things which already exist in custom keyboard firmware like QMK are good candidates for inclusion.

See CONTRIBUTING. IRC Channel: #keyd on oftc

联系我们 contact @ memedata.com