parse_keyboard_input

Function parse_keyboard_input 

Source
pub fn parse_keyboard_input(input: &str) -> Result<KeyboardInput, KeyParseError>
Expand description

Parse any keyboard input (key or modifier) with alias support

Examples found in repository?
examples/game_input_system.rs (line 70)
69    fn add_key_binding(bindings: &mut HashMap<Key, GameAction>, input: &str, action: GameAction) {
70        match parse_keyboard_input(input) {
71            Ok(KeyboardInput::Key(key)) => {
72                bindings.insert(key, action);
73                println!("  绑定: '{}' -> {:?}", input, action);
74            }
75            Ok(KeyboardInput::Modifier(_)) => {
76                eprintln!("警告: '{}' 是修饰键,请使用 add_modifier_binding", input);
77            }
78            Err(e) => {
79                eprintln!("警告: 无法解析键绑定 '{}': {}", input, e);
80            }
81        }
82    }
83
84    // 添加修饰键绑定
85    fn add_modifier_binding(
86        bindings: &mut HashMap<Modifier, GameAction>,
87        input: &str,
88        action: GameAction,
89    ) {
90        match parse_keyboard_input(input) {
91            Ok(KeyboardInput::Modifier(modifier)) => {
92                bindings.insert(modifier, action);
93                println!("  绑定: '{}' -> {:?}", input, action);
94            }
95            Ok(KeyboardInput::Key(_)) => {
96                eprintln!("警告: '{}' 是普通键,请使用 add_key_binding", input);
97            }
98            Err(e) => {
99                eprintln!("警告: 无法解析修饰键绑定 '{}': {}", input, e);
100            }
101        }
102    }
More examples
Hide additional examples
examples/validation_tool.rs (line 19)
7fn main() {
8    println!("=== 验证工具示例 ===\n");
9
10    // 测试往返转换
11    let test_inputs = ["a", "ctrl", "esc", "return", "lctrl"];
12    let platform = current_platform();
13    println!("当前平台: {}", platform);
14    println!("往返转换测试:");
15    for input in test_inputs {
16        println!("\n测试: '{}'", input);
17
18        // 字符串 -> 枚举
19        if let Ok(keyboard_input) = parse_keyboard_input(input) {
20            println!("  解析: {}", keyboard_input);
21
22            // 枚举 -> 虚拟键码 (使用当前平台)
23            let vk_code = keyboard_input.to_code(platform);
24            println!("  {} VK: 0x{:02X}", platform, vk_code);
25
26            // 虚拟键码 -> 枚举
27            if let Some(round_tripped) = KeyboardInput::from_code(vk_code, platform) {
28                println!("  往返: {}", round_tripped);
29
30                if keyboard_input == round_tripped {
31                    println!("  ✓ 往返转换成功");
32                } else {
33                    println!("  ✗ 往返转换失败");
34                }
35            }
36        } else {
37            println!("  ✗ 解析失败");
38        }
39    }
40
41    println!("\n示例完成!");
42}
examples/basic_usage.rs (line 25)
9fn main() {
10    println!("=== 键盘代码库基础用法示例 ===\n");
11
12    // 1. 基本键解析
13    println!("1. 基本键解析:");
14    let key: Key = "Enter".parse().unwrap();
15    println!(
16        "   'Enter' -> {:?} -> Windows VK: {:02X}",
17        key,
18        key.to_code(Platform::Windows)
19    );
20
21    // 2. 别名支持
22    println!("\n2. 别名支持:");
23    let inputs = ["ctrl", "esc", "return", "del", "cmd"];
24    for input in inputs {
25        if let Ok(ki) = parse_keyboard_input(input) {
26            println!(
27                "   '{}' -> {} -> VK: {:02X}",
28                input,
29                ki,
30                ki.to_code(Platform::Windows)
31            );
32        }
33    }
34
35    // 3. 快捷方式解析
36    println!("\n3. 快捷方式解析:");
37    let shortcuts = ["ctrl+c", "shift+tab", "ctrl+alt+del", "cmd+q"];
38    for shortcut in shortcuts {
39        if let Ok(parsed) = parse_shortcut_with_aliases(shortcut) {
40            println!("   '{}' -> {}", shortcut, parsed);
41        }
42    }
43
44    // 4. 跨平台代码转换
45    println!("\n4. 跨平台代码转换:");
46    let key = Key::A;
47    println!("   Key::A 代码:");
48    println!("   - Windows: {:02X}", key.to_code(Platform::Windows));
49    println!("   - Linux: {}", key.to_code(Platform::Linux));
50    println!("   - macOS: {}", key.to_code(Platform::MacOS));
51
52    // 5. 当前平台检测
53    println!("\n5. 当前平台: {}", current_platform());
54}
examples/macro_system.rs (line 37)
9fn main() {
10    println!("=== 宏系统示例 ===\n");
11
12    let platform = current_platform();
13    println!("当前平台: {}\n", platform);
14
15    // 演示宏录制概念
16    println!("宏录制概念演示:");
17
18    // 定义一些宏步骤 - 修复解析问题
19    let macro_steps = [
20        ("输入文本", "Hello", MacroActionType::Text),
21        ("快捷键", "ctrl+a", MacroActionType::Shortcut),
22        ("快捷键", "ctrl+c", MacroActionType::Shortcut),
23        ("导航", "arrowdown", MacroActionType::SingleKey),
24        ("快捷键", "ctrl+v", MacroActionType::Shortcut),
25        ("功能键", "f5", MacroActionType::SingleKey),
26        ("修饰键", "shift", MacroActionType::SingleKey),
27    ];
28
29    for (i, (action, input, action_type)) in macro_steps.iter().enumerate() {
30        println!("  步骤 {}: {} -> '{}'", i + 1, action, input);
31
32        match action_type {
33            MacroActionType::Text => {
34                // 文本输入 - 分解为单个字符
35                println!("      -> 文本输入: '{}'", input);
36                for ch in input.chars() {
37                    if let Ok(keyboard_input) = parse_keyboard_input(&ch.to_string()) {
38                        let vk_code = keyboard_input.to_code(platform);
39                        println!(
40                            "        字符 '{}' -> {} (VK: 0x{:02X})",
41                            ch, keyboard_input, vk_code
42                        );
43                    }
44                }
45            }
46            MacroActionType::Shortcut => {
47                // 快捷方式解析
48                match parse_shortcut_with_aliases(input) {
49                    Ok(shortcut) => {
50                        let modifier_codes: Vec<String> = shortcut
51                            .modifiers
52                            .iter()
53                            .map(|m| format!("0x{:02X}", m.to_code(platform)))
54                            .collect();
55                        let key_code = shortcut.key.to_code(platform);
56                        println!(
57                            "      -> 快捷方式: {} (修饰键: {:?}, 主键: 0x{:02X})",
58                            shortcut, modifier_codes, key_code
59                        );
60                    }
61                    Err(e) => {
62                        println!("      -> 解析失败: {}", e);
63                    }
64                }
65            }
66            MacroActionType::SingleKey => {
67                // 单个键解析
68                match parse_keyboard_input(input) {
69                    Ok(keyboard_input) => {
70                        let vk_code = keyboard_input.to_code(platform);
71                        println!("      -> {} (VK: 0x{:02X})", keyboard_input, vk_code);
72                    }
73                    Err(e) => {
74                        println!("      -> 解析失败: {}", e);
75                    }
76                }
77            }
78        }
79    }
80
81    // 演示宏回放概念
82    println!("\n宏回放概念演示:");
83
84    let playback_macro = vec![
85        MacroStep::Shortcut(parse_shortcut_with_aliases("ctrl+a").unwrap()),
86        MacroStep::Shortcut(parse_shortcut_with_aliases("ctrl+c").unwrap()),
87        MacroStep::Key(Key::ArrowDown),
88        MacroStep::Shortcut(parse_shortcut_with_aliases("ctrl+v").unwrap()),
89    ];
90
91    for (i, step) in playback_macro.iter().enumerate() {
92        println!("  执行步骤 {}: {}", i + 1, step);
93    }
94
95    println!("\n示例完成!");
96}