keyboard-codes 0.3.0

Cross-platform keyboard key code mapping and conversion
Documentation
//! 运行: cargo run --example keyboard_input_usage

use keyboard_codes::{current_platform, KeyParseError, KeyboardInput};

fn main() -> Result<(), KeyParseError> {
    // 1. 从字符串创建 KeyboardInput
    let key_a = "A".parse::<KeyboardInput>()?;
    let modifier_ctrl = "Control".parse::<KeyboardInput>()?;
    let _alias_esc = "esc".parse::<KeyboardInput>()?; // 使用别名

    // 2. 类型检查与转换
    println!("key_a 是普通键: {}", key_a.is_key()); // true
    println!("modifier_ctrl 是修饰键: {}", modifier_ctrl.is_modifier()); // true

    if let Some(key) = key_a.as_key() {
        println!("获取内部 Key: {}", key);
    }

    // 3. 平台键码转换
    let platform = current_platform();
    let code_a = key_a.to_code(platform);
    println!("A 键在 {} 的键码: 0x{:02X}", platform, code_a);

    // 4. 从键码反解析
    if let Some(parsed) = KeyboardInput::from_code(code_a, platform) {
        println!("从键码反解析: {}", parsed);
    }

    // 5. 高级解析(带别名和大小写不敏感)
    let inputs = ["Ctrl", "SHIFT", "alt", "F1", "space"];
    println!("\n高级解析测试:");
    for input in inputs {
        match KeyboardInput::parse_with_aliases(input) {
            Ok(kb_input) => println!("  '{}' -> {}", input, kb_input),
            Err(e) => println!("  '{}' 解析失败: {}", input, e),
        }
    }

    // 6. 显示实现
    println!("\nDisplay 实现:");
    println!("{} + {} = 组合键", modifier_ctrl, key_a);

    Ok(())
}