use keyboard_codes::{KeyParseError, KeyboardInput, Platform};
use std::thread;
use std::time::Duration;
pub fn typing_string(text: &str) -> Result<(), KeyParseError> {
let input = text.parse::<KeyboardInput>()?;
let platform = Platform::current();
let vk_code = input.to_code(platform);
simulate_key_press(vk_code)?;
Ok(())
}
fn simulate_key_press(vk_code: usize) -> Result<(), KeyParseError> {
println!("[模拟] 按下键码: 0x{:02X}", vk_code);
thread::sleep(Duration::from_millis(50));
println!("[模拟] 释放键码: 0x{:02X}", vk_code);
Ok(())
}
fn main() {
let test_sequence = ["ctrl", "a", "c", "v", "shift", "1"];
println!("开始模拟键盘输入序列:");
for key in test_sequence {
if let Err(e) = typing_string(key) {
println!("输入 '{}' 失败: {}", key, e);
continue;
}
println!("成功模拟: {}", key);
}
}