basic_usage/
basic_usage.rs

1//! 基础用法示例 - 展示核心功能
2
3// use keyboard_codes::*;
4use keyboard_codes::{
5    current_platform, parse_keyboard_input, parse_shortcut_with_aliases, Key, KeyCodeMapper,
6    Platform,
7};
8
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}