随机按键

This commit is contained in:
zqm
2026-04-08 10:02:53 +08:00
parent a69243c2ac
commit 35ba667583
16 changed files with 422 additions and 374 deletions

View File

@@ -29,16 +29,21 @@ extern "C" {
#[no_mangle]
extern "C" fn rust_app_main() {
// 初始化代码
send_a_key();
return;
loop {
send_random_key();
// 等待 5 秒
unsafe {
rust_vTaskDelay(5000 * PD_MS_TO_TICKS);
}
}
}
#[export_name = "rust_usb_callback"]
pub unsafe extern "C" fn usb_callback(_event: u32) {
}
/// 发送 A 键的按键事件
fn send_a_key() {
/// 发送 A-Z 随机键的按键事件
fn send_random_key() {
// 等待 USB 设备挂载
let mut mounted = false;
for _ in 0..10 {
@@ -55,8 +60,12 @@ fn send_a_key() {
}
if mounted {
// 发送 A 键按下事件
let keycode = [HID_KEY_A, 0, 0, 0, 0, 0];
// 生成随机的 A-Z 键码
let random_value = get_random_u32() % 26;
let random_key = HID_KEY_A + (random_value as u8);
// 发送随机键按下事件
let keycode = [random_key, 0, 0, 0, 0, 0];
unsafe {
rust_send_keyboard_report(REPORT_ID_KEYBOARD, 0, keycode.as_ptr());
}
@@ -66,10 +75,20 @@ fn send_a_key() {
rust_vTaskDelay(50 * PD_MS_TO_TICKS);
}
// 发送 A 键释放事件
// 发送随机键释放事件
let keycode = [0, 0, 0, 0, 0, 0];
unsafe {
rust_send_keyboard_report(REPORT_ID_KEYBOARD, 0, keycode.as_ptr());
}
}
}
/// 获取随机数
fn get_random_u32() -> u32 {
// 使用简单的线性同余生成器
static mut SEED: u32 = 42;
unsafe {
SEED = SEED.wrapping_mul(1103515245).wrapping_add(12345);
SEED
}
}