This crate delivers compile-time static type bindings for key values in web-sys::KeyboardEvent
.
use wasm_keys::WasmKey;
fn SomeComponent() {
let pressed_keys: Vec<WasmKey> = vec![];
let keydown_listener = wasm_bindgen::closure::Closure::wrap(
Box::new(move |event: web_sys::KeyboardEvent| {
pressed_keys.push(WasmKey::from(event.key()));
}) as Box<dyn Fn(_)>);
}
At its core, this crate introduces the WasmKey
enum, written to reflect every key value listed in the MDN KeyboardEvent
documentation. In addition, this crate provides a direct mapping from KeyboardEvent.key()
to WasmKey
upon compile-time.
This crate offers a feature to build a Hotkey
, a type that consists of one or many WasmKeys
. Specific modifier keys are noted as such.
Hotkey
implements the Eq
trait, allowing nice deep equality checks:
use wasm_keys::{WasmKey, Hotkey};
fn compare(hotkey: Hotkey, keys: Vec<WasmKey>) -> bool {
hotkey == Hotkey::from_keys(keys);
}
leptos-hotkeys - a hotkey library for Leptos, a web framework written in Rust.