Skip to content

compile-time static type bindings for key values in `web-sys::KeyboardEvent`

License

Notifications You must be signed in to change notification settings

gaucho-labs/wasm-keys

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

wasm-keys

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(_)>);
}

Typing

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.

Hotkeys

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);
}

References

Usages

leptos-hotkeys - a hotkey library for Leptos, a web framework written in Rust.

About

compile-time static type bindings for key values in `web-sys::KeyboardEvent`

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages