typing_simulation/
typing_simulation.rs1use keyboard_codes::{KeyParseError, KeyboardInput, Platform};
4use std::thread;
5use std::time::Duration;
6
7pub fn typing_string(text: &str) -> Result<(), KeyParseError> {
9 let input = text.parse::<KeyboardInput>()?;
11
12 let platform = Platform::current();
14 let vk_code = input.to_code(platform);
15
16 simulate_key_press(vk_code)?;
18
19 Ok(())
20}
21
22fn simulate_key_press(vk_code: usize) -> Result<(), KeyParseError> {
24 println!("[模拟] 按下键码: 0x{:02X}", vk_code);
26 thread::sleep(Duration::from_millis(50)); println!("[模拟] 释放键码: 0x{:02X}", vk_code);
34 Ok(())
35}
36
37fn main() {
38 let test_sequence = ["ctrl", "a", "c", "v", "shift", "1"];
39
40 println!("开始模拟键盘输入序列:");
41 for key in test_sequence {
42 if let Err(e) = typing_string(key) {
43 println!("输入 '{}' 失败: {}", key, e);
44 continue;
45 }
46 println!("成功模拟: {}", key);
47 }
48}