#键盘事件 #事件 #键盘 #前端 #键事件

wasm_keyboard

使用web_sys在Rust中处理键事件

2个版本

0.1.1 2023年2月23日
0.1.0 2023年2月19日

#827 in WebAssembly

MIT/Apache

14KB
108 代码行

wasm_keyboard

Latest Version Downloads Documentation License Dependency Status

使用WASMweb-sys简化WASM中的键盘事件管理。

用法

将以下内容添加到您的Cargo.toml

[dependencies]
wasm_keyboard = "0.1"

示例

use std::rc::Rc;

use wasm_bindgen::prelude::*;
use wasm_keyboard::{
    macros::{new_simplified_key_handler, start_keywise_keyboard_handler},
    uievents_code::{KeyboardEventCode, KEY_W},
};
use web_sys::KeyboardEvent;

// Called when the wasm module is instantiated
#[wasm_bindgen(start)]
fn main() -> Result<(), JsValue> {
    // Use `web_sys`'s global `window` function to get a handle on the global
    // window object.
    let window = web_sys::window().expect("no global `window` exists");
    let document = Rc::new(window.document().expect("should have a document on window"));
    let body = Rc::new(document.body().expect("document should have a body"));

    let w_handler = new_simplified_key_handler!(
        KeyboardEventCode::KeyW,
        state = (),
        keydown = {
            let body = body.clone();
            let document = document.clone();
            move |_state| {
                let val = document.create_element("p").unwrap();
                val.set_inner_html("W pressed down!");
                body.append_child(&val).unwrap();
            }
        },
        keyup = {
            let body = body.clone();
            let document = document.clone();
            move |_state| {
                let val = document.create_element("p").unwrap();
                val.set_inner_html("W released!");
                body.append_child(&val).unwrap();
            }
        }
    );

    start_keywise_keyboard_handler!(kh: Kh, document, [KEY_W => w_handler]);

    // Manufacture the element we're gonna append
    let val = document.create_element("p")?;
    val.set_inner_html("Hello from Rust!");

    body.append_child(&val)?;

    Ok(())
}

请参阅整个示例:https://github.com/JohnScience/wasm_keyboard_example.

SemVer策略

目前没有SemVer保证。该包正在被被动开发。

许可证

根据您的选择,许可协议为Apache License, Version 2.0MIT许可证
除非您明确声明,否则您有意提交以包含在此包中的任何贡献,根据Apache-2.0许可证定义,均应按上述方式双许可,不附加任何额外条款或条件。

依赖关系

~9.5MB
~188K SLoC