pub fn current_platform() -> PlatformExpand description
Get the current platform based on compilation target
Examples found in repository?
examples/gui_shortcuts.rs (line 121)
118fn main() {
119 println!("=== GUI 应用快捷键系统示例 ===\n");
120
121 let platform = current_platform();
122 let shortcut_manager = ShortcutManager::new(platform);
123
124 shortcut_manager.print_shortcut_reference();
125
126 // 模拟快捷键输入
127 println!("\n模拟快捷键处理:");
128 let test_combinations = [
129 (vec![0x11], 0x53), // Ctrl + S (Save)
130 (vec![0x11], 0x43), // Ctrl + C (Copy)
131 (vec![0x11, 0x10], 0x53), // Ctrl + Shift + S (Save As)
132 ];
133
134 for (modifiers, key) in test_combinations {
135 if let Some(command) = shortcut_manager.get_command(&modifiers, key) {
136 println!(" {:02X?} + {:02X} -> {:?}", modifiers, key, command);
137 } else {
138 println!(" {:02X?} + {:02X} -> 无匹配命令", modifiers, key);
139 }
140 }
141}More examples
examples/config_management.rs (line 11)
7fn main() {
8 println!("=== 配置管理示例 ===\n");
9
10 // 演示平台特定的配置
11 let platform = current_platform();
12 println!("当前平台: {}", platform);
13
14 // 演示快捷键验证
15 let test_shortcuts = ["ctrl+s", "cmd+q", "alt+f4", "ctrl+shift+esc"];
16
17 println!("快捷键验证:");
18 for shortcut in test_shortcuts {
19 match parse_shortcut_with_aliases(shortcut) {
20 Ok(parsed) => {
21 let vk_codes: Vec<_> = parsed
22 .modifiers
23 .iter()
24 .map(|m| m.to_code(platform))
25 .chain(std::iter::once(parsed.key.to_code(platform)))
26 .collect();
27 println!(" ✓ '{}' -> VK: {:02X?}", shortcut, vk_codes);
28 }
29 Err(e) => {
30 println!(" ✗ '{}' -> 错误: {}", shortcut, e);
31 }
32 }
33 }
34
35 println!("\n示例完成!");
36}examples/validation_tool.rs (line 12)
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/keyboard_input_usage.rs (line 20)
5fn main() -> Result<(), KeyParseError> {
6 // 1. 从字符串创建 KeyboardInput
7 let key_a = "A".parse::<KeyboardInput>()?;
8 let modifier_ctrl = "Control".parse::<KeyboardInput>()?;
9 let _alias_esc = "esc".parse::<KeyboardInput>()?; // 使用别名
10
11 // 2. 类型检查与转换
12 println!("key_a 是普通键: {}", key_a.is_key()); // true
13 println!("modifier_ctrl 是修饰键: {}", modifier_ctrl.is_modifier()); // true
14
15 if let Some(key) = key_a.as_key() {
16 println!("获取内部 Key: {}", key);
17 }
18
19 // 3. 平台键码转换
20 let platform = current_platform();
21 let code_a = key_a.to_code(platform);
22 println!("A 键在 {} 的键码: 0x{:02X}", platform, code_a);
23
24 // 4. 从键码反解析
25 if let Some(parsed) = KeyboardInput::from_code(code_a, platform) {
26 println!("从键码反解析: {}", parsed);
27 }
28
29 // 5. 高级解析(带别名和大小写不敏感)
30 let inputs = ["Ctrl", "SHIFT", "alt", "F1", "space"];
31 println!("\n高级解析测试:");
32 for input in inputs {
33 match KeyboardInput::parse_with_aliases(input) {
34 Ok(kb_input) => println!(" '{}' -> {}", input, kb_input),
35 Err(e) => println!(" '{}' 解析失败: {}", input, e),
36 }
37 }
38
39 // 6. 显示实现
40 println!("\nDisplay 实现:");
41 println!("{} + {} = 组合键", modifier_ctrl, key_a);
42
43 Ok(())
44}examples/basic_usage.rs (line 53)
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/game_input_system.rs (line 172)
169fn main() {
170 println!("=== 游戏输入系统示例 ===\n");
171
172 let platform = current_platform();
173 println!("当前平台: {}", platform);
174
175 let input_mapper = GameInputMapper::new(platform);
176
177 input_mapper.list_bindings();
178
179 println!("\n模拟按键事件 (Windows VK 代码):");
180 let test_keys = [
181 (0x57, "W"), // W
182 (0x41, "A"), // A
183 (0x20, "Space"), // Space
184 (0x31, "1"), // 1
185 (0x10, "Shift"), // Shift
186 (0x11, "Ctrl"), // Ctrl
187 ];
188
189 for &(vk_code, desc) in &test_keys {
190 if let Some(action) = input_mapper.handle_key_event(vk_code) {
191 println!(" VK {:02X} ({:6}) -> {:?}", vk_code, desc, action);
192 } else {
193 println!(" VK {:02X} ({:6}) -> 无绑定", vk_code, desc);
194 }
195 }
196
197 // 测试组合键
198 println!("\n模拟组合键事件:");
199 let combo_tests = [
200 (vec![0x10], 0x57, "Shift + W"), // Shift + W
201 (vec![0x11], 0x41, "Ctrl + A"), // Ctrl + A
202 ];
203
204 for (modifiers, key, desc) in combo_tests {
205 if let Some((active_modifiers, action)) = input_mapper.handle_combo_event(&modifiers, key) {
206 let mod_names: Vec<String> = active_modifiers.iter().map(|m| m.to_string()).collect();
207 println!(" {} -> {:?} (修饰键: {:?})", desc, action, mod_names);
208 } else {
209 println!(" {} -> 无绑定", desc);
210 }
211 }
212
213 println!("\n示例完成!");
214}Additional examples can be found in: