1#![windows_subsystem = "windows"]
2
3use std::sync::mpsc;
4use std::{collections::HashMap, time::Duration};
5
6use device_query::{DeviceEvents, DeviceEventsHandler, Keycode};
7use quad_snd::{AudioContext, Sound};
8
9fn main() {
10 let ctx = AudioContext::new();
11 let sounds = std::fs::read_dir("sounds")
12 .expect("cant read sounds")
13 .flat_map(|f| {
14 let p = f.ok()?.path();
15 let n = p.file_stem()?.to_string_lossy().into_owned();
16 (n != "LICENSE" && n != "README").then(|| {
17 (
18 n,
19 Sound::load(&ctx, &std::fs::read(p).expect("can't load sound")),
20 )
21 })
22 })
23 .collect::<HashMap<String, Sound>>();
24 let play_sound = |name: &str| {
25 let sound = sounds.get(name).unwrap();
26 sound.play(&ctx, Default::default());
27 };
28
29 let (tx, rx) = mpsc::channel();
30
31 let query = DeviceEventsHandler::new(Duration::from_millis(10)).expect("already running");
32 let _guard = query.on_key_down(move |key| {
33 tx.send(*key).expect("tx");
34 });
35
36 loop {
37 if let Ok(key) = rx.recv() {
38 match key {
39 Keycode::CapsLock => play_sound("caps"),
40 Keycode::Delete | Keycode::Backspace => play_sound("delete"),
41 Keycode::Up | Keycode::Down | Keycode::Left | Keycode::Right => {
42 play_sound("movement")
43 }
44 _ => {
45 let no = fastrand::u8(1..=4);
46 play_sound(&format!("press-{no}"));
47 }
48 }
49 }
50 println!("handling new event");
51 }
52}