Skip to main content

Crate xkb_type

Crate xkb_type 

Source
Expand description

Virtual keyboard typing via evdev/uinput or X11 XTEST with XKB layout-aware key mapping.

This crate provides:

  • Keyboard — a virtual uinput device that injects keystrokes (or, with the xtest feature, an X11 XTEST connection).
  • XkbKeymap — reverse char→keycode lookup from the active XKB layout.
  • ClipboardBackend — trait + auto-detected clipboard implementations.

§Example (auto-detect layout and clipboard)

use xkb_type::{Keyboard, Key};
use std::time::Duration;

let mut kb = Keyboard::new(Duration::from_millis(2))?;
kb.type_text("Hello — こんにちは — €100 — 😀")?;
kb.backspace(5)?;
kb.send_combo(&[Key::KEY_LEFTCTRL, Key::KEY_C])?;

§Example (explicit layout)

use xkb_type::Keyboard;
use std::time::Duration;

let mut kb = Keyboard::with_layout("de", None, Duration::from_millis(5))?;
kb.type_text("Schöne Grüße")?;

§Example (pause or stop between keys)

KeyInjector::type_text asks a gate before every key, with the byte offset of the next character. The gate may block (to wait for something) and can stop the typing; the result says where it stopped.

use std::ops::ControlFlow;
use std::time::Duration;
use xkb_type::{KeyInjector, Keyboard, Typed};

let mut kb: Box<dyn KeyInjector> = Box::new(Keyboard::new(Duration::from_millis(2))?);
let text = "hello world";
// Stop before the space.
let mut gate = |at: usize| {
    if at < 5 {
        ControlFlow::Continue(())
    } else {
        ControlFlow::Break(())
    }
};
match kb.type_text(text, &mut gate)? {
    Typed::All => println!("typed everything"),
    Typed::StoppedAt(at) => println!("not typed: {:?}", &text[at..]),
}

Re-exports§

pub use clipboard::default_clipboard;
pub use clipboard::NoopClipboard;
pub use clipboard::WaylandClipboard;
pub use clipboard::X11Clipboard;
pub use keyboard::Keyboard;
pub use keymap::KeyboardLayout;
pub use keymap::XkbKeymap;
pub use evdev;

Modules§

clipboard
Clipboard implementations — Wayland (wl-copy/wl-paste), X11 (arboard), and noop.
keyboard
Virtual keyboard via evdev/uinput, or via XTEST on X11.
keymap
XKB reverse lookup table: char → (Keycode, Modifiers).

Structs§

Key
Scancodes for key presses.
KeyMapping
Information needed to produce a character at the cursor.
KeyTap
A single keypress with optional Shift and/or AltGr modifiers.

Enums§

Typed
How far a KeyInjector::type_text call got.

Traits§

ClipboardBackend
Trait for clipboard get/set operations.
KeyInjector
High-level text-injection abstraction.

Functions§

keyboard_from_detected_layout
Convenience: build a Keyboard from the detected layout with a sensible default key delay (5 ms).