Skip to content

Repository files navigation

wkb — Wayland Keyboard

Crates.io Documentation License Test Status

A lightweight, pure Rust keyboard handling library for Wayland. WKB is a drop-in alternative to xkbcommon that compiles XKB keymaps, tracks modifier and compose state, and maps evdev key codes to characters — all without C dependencies.

Features

  • XKB keymap import — parse keymap strings received from Wayland compositors via wl_keyboard.
  • XKB keymap export — serialize keymaps back to XKB v1 text format via as_xkb_string().
  • Full modifier support — Shift, Ctrl, Alt, AltGr, Caps Lock, Num Lock, Scroll Lock, and multi-level keys.
  • Compose sequences — built-in compose key and automatic compose handling.
  • Multi-layout keymaps — supports multiple layouts with group switching.
  • LED state — query Caps/Num/Scroll Lock indicator state.
  • Repeat info — query whether a key repeats.
  • Lightweight — no C FFI, no unsafe beyond Send/Sync impls, minimal dependencies.

Quick Start

[dependencies]
wayland-keyboard = "0.1"
use wkb::WKB;

// Build from an XKB keymap string (e.g. received from a Wayland compositor)
let keymap_string = std::fs::read_to_string("/path/to/keymap").unwrap();
let mut wkb = WKB::new_from_string(&keymap_string).unwrap();

// Process a key press (evdev code 38 = 'a' on US layout)
let result = wkb.press_key(38);
println!("keysym: {:#x}, compose: {:?}", result.keysym, result.compose);

// Release the key
let result = wkb.release_key(38);

// Query current modifier state
let mods = wkb.modifiers_state();
println!("ctrl={} alt={} shift={}", mods.ctrl, mods.alt, mods.shift);

Key Event API

Method Mutates state Use case
press_key(evdev) yes Key down — updates modifiers, advances compose
release_key(evdev) yes Key up — updates modifiers
repeat_key(evdev) yes Key repeat — advances compose
key_char(evdev) no Raw character under current modifiers (no compose)

All three event methods return a KeyResult containing the keysym, compose state, and whether the key is a modifier.

Compositor Usage

use wkb::WKB;

// Build from RMLVO names (compositor side)
let wkb = WKB::new_from_names("evdev", "pc105", "us,de", "dvorak,", None).unwrap();

// Serialize to XKB string for wl_keyboard.keymap
let xkb_string = wkb.as_xkb_string().unwrap();

// Switch layouts via group index (no re-parsing needed)
// wkb.set_layout(1).unwrap(); // switch to German

Feature Flags

Flag Default Description
xkb yes XKB keymap compilation via the xkb-core crate
compose yes Compose-key / dead-key sequence support

Benchmarks

Last updated: 2026-08-07 (automated via CI)

Speed

Benchmark wkb wkb-noxkb xkbcommon xkbcommon-dl vs xkbcommon
Setup (no compose) 18.40 ms 838.2 µs 2.17 ms 2.18 ms 8.5x slower
Setup (with compose) 18.48 ms 841.7 µs 4.03 ms 4.01 ms 4.6x slower
Key update 89 ns 88 ns 168 ns 168 ns 1.9x faster
Get char 95 ns 95 ns 306 ns 273 ns 3.2x faster
Get keysym 97 ns 95 ns 208 ns 207 ns 2.1x faster
Compose feed 255 ns 256 ns 542 ns 506 ns 2.1x faster

Memory

Library Peak RSS
wkb 6.5 MB
wkb-noxkb 3.2 MB
xkbcommon 6.7 MB
xkbcommon-dl 6.7 MB

Binary Size

Sizes for xkbcommon and xkbcommon-dl include the dynamically-linked libxkbcommon.so.

Binary Size (stripped)
wkb (no XKB) 585 KB
wkb (with XKB) 820 KB
xkbcommon 723 KB
xkbcommon-dl 756 KB

Scope and Limitations

WKB targets the subset of XKB used by Wayland clients and compositors. Geometry descriptions and other X11-only features are intentionally out of scope.

License

MIT

About

Writing a replacement for xkb for wayland

Resources

Stars

15 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages