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
Those strings are usually provided by a configuration file.
rust
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
assert_eq!(
crokey::parse("alt-enter").unwrap(),
KeyEvent::new(KeyCode::Enter, KeyModifiers::ALT),
);
assert_eq!(
crokey::parse("shift-F6").unwrap(),
KeyEvent::new(KeyCode::F(6), KeyModifiers::SHIFT),
);
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.
rust
match key_event {
key!(ctrl-c) => {
println!("Arg! You savagely killed me with a {}", fmt.to_string(key_event).red());
break;
}
key!(ctrl-q) => {
println!("You typed {} which gracefully quits", fmt.to_string(key_event).green());
break;
}
_ => {
println!("You typed {}", fmt.to_string(key_event).blue());
}
}
Complete example in /examples/print_key
:
```rust use crokey::*; use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
// The default format let format = KeyEventFormat::default(); asserteq!(format.tostring(key!(shift-a)), "Shift-a"); asserteq!(format.tostring(key!(ctrl-c)), "Ctrl-c");
// A more compact format let format = KeyEventFormat::default() .withimplicitshift() .withcontrol("^"); asserteq!(format.tostring(key!(shift-a)), "A"); asserteq!(format.to_string(key!(ctrl-c)), "^c"); ```
With the "serde" feature enabled, you can read configuration files in a direct way:
``` use { crokey::*, crossterm::event::KeyEvent, serde::Deserialize, std::collections::HashMap, };
struct Config {
keybindings: HashMap
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
.
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 |