GitHub stars GitHub stars

keymap-rs

A Rust library for parsing key sequences from text or configuration files (TOML, YAML, etc.) and mapping them to actions based on keyboard input from backends like crossterm, termion, wasm, and others.

DEMO: Nyan Jump! (WASM Edition)

GAME OVER!

EXAMPLE: Derive Macro

Define an enum and automatically derive key bindings using the #[derive(KeyMap)] macro.

#[derive(keymap::KeyMap, Deserialize, Hash, PartialEq, Eq)]
pub enum Action {
  /// Jump over obstacles
  #[key("space")]
  Jump,

  /// Move leftward
  #[key("left")]
  Left,

  /// Move rightward
  #[key("right")]
  Right,

  /// Pause
  #[key("p")]
  Pause,

  /// Restart
  #[key("q", "esc")]
  Quit,
}

EXAMPLE: Key Override

Customize or extend key bindings through configuration files without changing the source code.

# Now both 'space' and the 'up' arrow can be used to jump
Jump = { keys = ["space", "up"], description = "Jump Jump!" }