when osu sound i guess

feat: held keys

ptr.pet 7d4e376a db9a0090

verified
Changed files
+59 -17
src
+59 -17
src/main.rs
···
#![windows_subsystem = "windows"]
-
use std::sync::mpsc;
+
use std::collections::HashSet;
use std::{collections::HashMap, time::Duration};
-
use device_query::{DeviceEvents, DeviceEventsHandler, Keycode};
+
use device_query::{DeviceState, Keycode};
use quad_snd::{AudioContext, Sound};
fn main() {
···
let sound = sounds.get(name).unwrap();
sound.play(&ctx, Default::default());
};
+
let play_sound_for_key = |key: Keycode| match key {
+
Keycode::CapsLock => play_sound("caps"),
+
Keycode::Delete | Keycode::Backspace => play_sound("delete"),
+
Keycode::Up | Keycode::Down | Keycode::Left | Keycode::Right => play_sound("movement"),
+
_ => {
+
let no = fastrand::u8(1..=4);
+
play_sound(&format!("press-{no}"));
+
}
+
};
-
let (tx, rx) = mpsc::channel();
+
let state = DeviceState::new();
+
let mut previously_held_keys = HashSet::<Keycode>::new();
+
let mut key_press_times = HashMap::<Keycode, std::time::Instant>::new();
+
let mut last_sound_time = std::time::Instant::now();
-
let query = DeviceEventsHandler::new(Duration::from_millis(10)).expect("already running");
-
let _guard = query.on_key_down(move |key| {
-
tx.send(*key).expect("tx");
-
});
+
let initial_delay = Duration::from_millis(500); // Wait 500ms before starting to repeat
+
let repeat_interval = Duration::from_millis(50); // Then repeat every 50ms
loop {
-
if let Ok(key) = rx.recv() {
-
match key {
-
Keycode::CapsLock => play_sound("caps"),
-
Keycode::Delete | Keycode::Backspace => play_sound("delete"),
-
Keycode::Up | Keycode::Down | Keycode::Left | Keycode::Right => {
-
play_sound("movement")
+
let currently_held_keys: HashSet<Keycode> = state.query_keymap().into_iter().collect();
+
+
// Track when keys were first pressed
+
for key in &currently_held_keys {
+
if !previously_held_keys.contains(key) {
+
// Key just pressed, record the time and play initial sound
+
key_press_times.insert(*key, std::time::Instant::now());
+
play_sound_for_key(*key);
+
}
+
}
+
+
// Remove timing info for released keys
+
key_press_times.retain(|key, _| currently_held_keys.contains(key));
+
+
// Play repeating sounds every 50ms, but only after initial delay
+
if last_sound_time.elapsed() >= repeat_interval {
+
let now = std::time::Instant::now();
+
for key in &currently_held_keys {
+
if matches!(
+
key,
+
Keycode::LShift
+
| Keycode::RShift
+
| Keycode::LControl
+
| Keycode::RControl
+
| Keycode::RAlt
+
| Keycode::LAlt
+
| Keycode::RMeta
+
| Keycode::LMeta
+
| Keycode::CapsLock
+
) {
+
continue;
}
-
_ => {
-
let no = fastrand::u8(1..=4);
-
play_sound(&format!("press-{no}"));
+
if let Some(press_time) = key_press_times.get(key) {
+
// Only repeat if key has been held longer than initial delay
+
if now.duration_since(*press_time) >= initial_delay {
+
play_sound_for_key(*key);
+
}
}
}
+
last_sound_time = now;
}
-
println!("handling new event");
+
+
previously_held_keys = currently_held_keys;
+
+
// Buffer inputs at lower interval (5ms)
+
std::thread::sleep(Duration::from_millis(5));
}
}