#key #parse


Parse and describe keys - helping incorporate keybindings in terminal applications

12 releases (4 breaking)

0.5.1 Aug 30, 2022
0.4.3 Aug 27, 2022
0.4.2 Jul 28, 2022
0.4.0 Feb 7, 2022

#143 in Command-line interface

Download history 485/week @ 2023-01-18 588/week @ 2023-01-25 782/week @ 2023-02-01 678/week @ 2023-02-08 742/week @ 2023-02-15 1475/week @ 2023-02-22 905/week @ 2023-03-01 975/week @ 2023-03-08 834/week @ 2023-03-15 841/week @ 2023-03-22 783/week @ 2023-03-29 650/week @ 2023-04-05 597/week @ 2023-04-12 581/week @ 2023-04-19 694/week @ 2023-04-26 630/week @ 2023-05-03

2,617 downloads per month
Used in 10 crates (9 directly)

MIT license

393 lines

MIT Latest Version docs Chat on Miaou


Crokey helps incorporate configurable keybindings in crossterm based terminal applications by providing functions

  • parsing key combinations from strings
  • describing key combinations in strings
  • parsing key combinations at compile time

Parse a string

Those strings are usually provided by a configuration file.

use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
    KeyEvent::new(KeyCode::Enter, KeyModifiers::ALT),
    KeyEvent::new(KeyCode::F(6), KeyModifiers::SHIFT),

Use key event "literals" thanks to procedural macros

Those key events are parsed at compile time and have zero runtime cost.

They're efficient and convenient for matching events or defining hardcoded keybindings.

match key_event {
    key!(ctrl-c) => {
        println!("Arg! You savagely killed me with a {}", fmt.to_string(key_event).red());
    key!(ctrl-q) => {
        println!("You typed {} which gracefully quits", fmt.to_string(key_event).green());
    _ => {
        println!("You typed {}", fmt.to_string(key_event).blue());

Complete example in /examples/print_key:


Display a string with a configurable format

use crokey::*;
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

// The default format
let format = KeyEventFormat::default();
assert_eq!(format.to_string(key!(shift-a)), "Shift-a");
assert_eq!(format.to_string(key!(ctrl-c)), "Ctrl-c");

// A more compact format
let format = KeyEventFormat::default()
assert_eq!(format.to_string(key!(shift-a)), "A");
assert_eq!(format.to_string(key!(ctrl-c)), "^c");

Deserialize keybindings using Serde

With the "serde" feature enabled, you can read configuration files in a direct way:

use {
struct Config {
    keybindings: HashMap<CroKey, String>,
static CONFIG_HJSON: &str = r#"
    keybindings: {
        a: aardvark
        shift-b: babirussa
        ctrl-k: koala
        alt-j: jaguar
let config: Config = deser_hjson::from_str(CONFIG_HJSON).unwrap();
let key_event: KeyEvent = key!(shift-b);

You can use any Serde compatible format such as JSON or TOML.

The CroKey wrapper type may be convenient as it implements FromStr, Deserialize, and Display, but its use is optional. The "deser_keybindings" example uses TOML and demonstrates how to have KeyEvent keys in the map instead of Crokey.

Crossterm Compatibility

Crokey includes Crossterm, so you don't have to import it and to avoid conflicts.

Different versions of Crossterm have different capabilities and you may need a specific version.

Here are the versions of Crossterm included in the currently maintained versions of Crokey:

crokey version crossterm version
0.4.x 0.23.3
0.5.x 0.24.0


~116K SLoC