add configurable keyup/down delay to USB typing methods

This commit is contained in:
bunnie 2023-05-22 17:21:50 +08:00
parent d3dfe9fdde
commit 3b5a78a908
3 changed files with 23 additions and 4 deletions

View file

@ -24,6 +24,8 @@ pub(crate) enum Opcode {
IsRestricted = 8, IsRestricted = 8,
/// Set-and-check of USB debug restriction /// Set-and-check of USB debug restriction
DebugUsbOp = 9, DebugUsbOp = 9,
/// Set autotype rate
SetAutotypeRate = 10,
/// Send a U2F message /// Send a U2F message
U2fTx = 128, U2fTx = 128,

View file

@ -259,6 +259,13 @@ impl UsbHid {
None => Err(xous::Error::UseBeforeInit), None => Err(xous::Error::UseBeforeInit),
} }
} }
/// Sets the autotype delay. Defaults to 30ms on boot, must be reset every time on reboot.
pub fn set_autotype_delay_ms(&self, rate: usize) {
send_message(
self.conn,
Message::new_scalar(Opcode::SetAutotypeRate.to_usize().unwrap(), rate, 0, 0, 0)
).unwrap(); // just unwrap it. If the send fails, we want to see the panic at this spot!
}
pub fn get_led_state(&self) -> Result<KeyboardLedsReport, xous::Error> { pub fn get_led_state(&self) -> Result<KeyboardLedsReport, xous::Error> {
match send_message( match send_message(
self.conn, self.conn,

View file

@ -309,6 +309,7 @@ pub(crate) fn main_hw() -> ! {
let mut lockstatus_force_update = true; // some state to track if we've been through a suspend/resume, to help out the status thread with its UX update after a restart-from-cold let mut lockstatus_force_update = true; // some state to track if we've been through a suspend/resume, to help out the status thread with its UX update after a restart-from-cold
let mut was_suspend = true; let mut was_suspend = true;
let mut autotype_delay_ms = 30;
#[cfg(feature="minimal")] #[cfg(feature="minimal")]
std::thread::spawn(move || { std::thread::spawn(move || {
@ -1026,11 +1027,11 @@ pub(crate) fn main_hw() -> ! {
let keyboard = composite.interface::<NKROBootKeyboardInterface<'_, _, _,>, _>(); let keyboard = composite.interface::<NKROBootKeyboardInterface<'_, _, _,>, _>();
keyboard.write_report(&codes).ok(); keyboard.write_report(&codes).ok();
keyboard.tick().ok(); keyboard.tick().ok();
tt.sleep_ms(30).ok(); tt.sleep_ms(autotype_delay_ms).ok();
if auto_up { if auto_up {
keyboard.write_report(&[]).ok(); // this is the key-up keyboard.write_report(&[]).ok(); // this is the key-up
keyboard.tick().ok(); keyboard.tick().ok();
tt.sleep_ms(30).ok(); tt.sleep_ms(autotype_delay_ms).ok();
} }
xous::return_scalar(msg.sender, 0).unwrap(); xous::return_scalar(msg.sender, 0).unwrap();
} else { } else {
@ -1074,6 +1075,15 @@ pub(crate) fn main_hw() -> ! {
_ => {} // do nothing; don't fail, don't report any error. _ => {} // do nothing; don't fail, don't report any error.
} }
} }
Some(Opcode::SetAutotypeRate) => msg_scalar_unpack!(msg, rate, _, _, _, {
// limit rate to 0.5s delay. Even then, this will probably cause repeated characters because
// it also adjusts keydown delays
let checked_rate = if rate > 500 {
500
} else { rate };
// there is no limit on the minimum rate. good luck if you set it to 0!
autotype_delay_ms = checked_rate;
}),
Some(Opcode::SendString) => { Some(Opcode::SendString) => {
let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
let mut usb_send = buffer.to_original::<api::UsbString, _>().unwrap(); let mut usb_send = buffer.to_original::<api::UsbString, _>().unwrap();
@ -1093,10 +1103,10 @@ pub(crate) fn main_hw() -> ! {
let keyboard = composite.interface::<NKROBootKeyboardInterface<'_, _, _,>, _>(); let keyboard = composite.interface::<NKROBootKeyboardInterface<'_, _, _,>, _>();
keyboard.write_report(&codes).ok(); keyboard.write_report(&codes).ok();
keyboard.tick().ok(); keyboard.tick().ok();
tt.sleep_ms(30).ok(); tt.sleep_ms(autotype_delay_ms).ok();
keyboard.write_report(&[]).ok(); // this is the key-up keyboard.write_report(&[]).ok(); // this is the key-up
keyboard.tick().ok(); keyboard.tick().ok();
tt.sleep_ms(30).ok(); tt.sleep_ms(autotype_delay_ms).ok();
sent += 1; sent += 1;
} }
} }