pub enum KeyboardInput {
Key(Key),
Modifier(Modifier),
}Expand description
Unified keyboard input type that can represent both keys and modifiers
Variants§
Implementations§
Source§impl KeyboardInput
impl KeyboardInput
Sourcepub fn to_code(&self, platform: Platform) -> usize
pub fn to_code(&self, platform: Platform) -> usize
Convert to platform-specific code
Examples found in repository?
examples/typing_simulation.rs (line 14)
8pub fn typing_string(text: &str) -> Result<(), KeyParseError> {
9 // 1. 解析输入字符串(支持别名)
10 let input = text.parse::<KeyboardInput>()?;
11
12 // 2. 获取当前平台虚拟键码
13 let platform = Platform::current();
14 let vk_code = input.to_code(platform);
15
16 // 3. 模拟键盘按下和释放
17 simulate_key_press(vk_code)?;
18
19 Ok(())
20}More examples
examples/validation_tool.rs (line 23)
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 21)
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 30)
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 38)
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}Sourcepub fn from_code(code: usize, platform: Platform) -> Option<Self>
pub fn from_code(code: usize, platform: Platform) -> Option<Self>
Parse from platform-specific code
Examples found in repository?
examples/game_input_system.rs (line 105)
104 fn handle_key_event(&self, vk_code: usize) -> Option<GameAction> {
105 if let Some(keyboard_input) = KeyboardInput::from_code(vk_code, self.platform) {
106 match keyboard_input {
107 KeyboardInput::Key(key) => {
108 // 查找普通键绑定
109 if let Some(action) = self.key_bindings.get(&key) {
110 return Some(*action);
111 }
112 }
113 KeyboardInput::Modifier(modifier) => {
114 // 查找修饰键绑定
115 if let Some(action) = self.modifier_bindings.get(&modifier) {
116 return Some(*action);
117 }
118 }
119 }
120 }
121 None
122 }
123
124 fn list_bindings(&self) {
125 println!("\n普通键绑定:");
126 let mut keys: Vec<_> = self.key_bindings.iter().collect();
127 keys.sort_by_key(|(key, _)| key.as_str());
128 for (key, action) in keys {
129 println!(" {:15} -> {:?}", key, action);
130 }
131
132 println!("\n修饰键绑定:");
133 let mut modifiers: Vec<_> = self.modifier_bindings.iter().collect();
134 modifiers.sort_by_key(|(modifier, _)| modifier.as_str());
135 for (modifier, action) in modifiers {
136 println!(" {:15} -> {:?}", modifier, action);
137 }
138 }
139
140 // 处理组合键(例如:Shift + 某个键)
141 fn handle_combo_event(
142 &self,
143 modifier_vks: &[usize],
144 key_vk: usize,
145 ) -> Option<(Vec<Modifier>, GameAction)> {
146 let modifiers: Vec<Modifier> = modifier_vks
147 .iter()
148 .filter_map(|&code| {
149 if let Some(KeyboardInput::Modifier(modifier)) =
150 KeyboardInput::from_code(code, self.platform)
151 {
152 Some(modifier)
153 } else {
154 None
155 }
156 })
157 .collect();
158
159 if let Some(KeyboardInput::Key(key)) = KeyboardInput::from_code(key_vk, self.platform) {
160 if let Some(action) = self.key_bindings.get(&key) {
161 return Some((modifiers, *action));
162 }
163 }
164
165 None
166 }More examples
examples/validation_tool.rs (line 27)
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 25)
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}Sourcepub fn is_key(&self) -> bool
pub fn is_key(&self) -> bool
Check if this is a regular key
Examples found in repository?
examples/keyboard_input_usage.rs (line 12)
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}Sourcepub fn is_modifier(&self) -> bool
pub fn is_modifier(&self) -> bool
Check if this is a modifier key
Examples found in repository?
examples/keyboard_input_usage.rs (line 13)
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}Sourcepub fn as_key(&self) -> Option<Key>
pub fn as_key(&self) -> Option<Key>
Get the inner key if this is a Key variant
Examples found in repository?
examples/keyboard_input_usage.rs (line 15)
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}Sourcepub fn as_modifier(&self) -> Option<Modifier>
pub fn as_modifier(&self) -> Option<Modifier>
Get the inner modifier if this is a Modifier variant
Sourcepub fn parse_with_aliases(input: &str) -> Result<Self, KeyParseError>
pub fn parse_with_aliases(input: &str) -> Result<Self, KeyParseError>
Parse with alias and case-insensitive support
Examples found in repository?
examples/keyboard_input_usage.rs (line 33)
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}Trait Implementations§
Source§impl Clone for KeyboardInput
impl Clone for KeyboardInput
Source§fn clone(&self) -> KeyboardInput
fn clone(&self) -> KeyboardInput
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for KeyboardInput
impl Debug for KeyboardInput
Source§impl Display for KeyboardInput
impl Display for KeyboardInput
Source§impl FromStr for KeyboardInput
impl FromStr for KeyboardInput
Source§impl Hash for KeyboardInput
impl Hash for KeyboardInput
Source§impl KeyCodeMapper for KeyboardInput
impl KeyCodeMapper for KeyboardInput
Source§impl PartialEq for KeyboardInput
impl PartialEq for KeyboardInput
impl Copy for KeyboardInput
impl Eq for KeyboardInput
impl StructuralPartialEq for KeyboardInput
Auto Trait Implementations§
impl Freeze for KeyboardInput
impl RefUnwindSafe for KeyboardInput
impl Send for KeyboardInput
impl Sync for KeyboardInput
impl Unpin for KeyboardInput
impl UnwindSafe for KeyboardInput
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more