···3939 let mut previously_held_keys = HashSet::<Keycode>::new();
4040 let mut key_press_times = HashMap::<Keycode, std::time::Instant>::new();
4141 let mut last_sound_time = std::time::Instant::now();
4242+ let mut sounds_enabled = true; // Toggle for enabling/disabling sounds
42434344 let initial_delay = Duration::from_millis(500); // Wait 500ms before starting to repeat
4445 let repeat_interval = Duration::from_millis(50); // Then repeat every 50ms
···4647 loop {
4748 let currently_held_keys: HashSet<Keycode> = state.query_keymap().into_iter().collect();
48494949- // Track when keys were first pressed
5050- for key in ¤tly_held_keys {
5151- if !previously_held_keys.contains(key) {
5252- // Key just pressed, record the time and play initial sound
5353- key_press_times.insert(*key, std::time::Instant::now());
5454- play_sound_for_key(*key);
5555- }
5656- }
5050+ // Check for toggle hotkey (Ctrl + Alt + L/R Shift + C)
5151+ let hotkey_combo = [
5252+ [Keycode::LControl, Keycode::RControl], // Either left or right control
5353+ [Keycode::LAlt, Keycode::RAlt], // Either left or right alt
5454+ [Keycode::LShift, Keycode::RShift], // Either left or right shift
5555+ [Keycode::C, Keycode::C], // C key (duplicated for array consistency)
5656+ ];
57575858- // Remove timing info for released keys
5959- key_press_times.retain(|key, _| currently_held_keys.contains(key));
5858+ let check_hotkey = |current: &HashSet<Keycode>, previous: &HashSet<Keycode>| {
5959+ hotkey_combo.iter().all(|key_group| {
6060+ key_group
6161+ .iter()
6262+ .any(|key| current.contains(key) || previous.contains(key))
6363+ })
6464+ };
60656161- // Play repeating sounds every 50ms, but only after initial delay
6262- if last_sound_time.elapsed() >= repeat_interval {
6363- let now = std::time::Instant::now();
6666+ let hotkey_active = check_hotkey(¤tly_held_keys, &previously_held_keys);
6767+ let hotkey_was_active = check_hotkey(&previously_held_keys, &HashSet::new());
6868+6969+ if hotkey_active && !hotkey_was_active {
7070+ sounds_enabled = !sounds_enabled;
7171+ }
7272+7373+ // Only process sound logic if sounds are enabled
7474+ if sounds_enabled {
7575+ // Track when keys were first pressed
6476 for key in ¤tly_held_keys {
6565- if matches!(
6666- key,
6767- Keycode::LShift
6868- | Keycode::RShift
6969- | Keycode::LControl
7070- | Keycode::RControl
7171- | Keycode::RAlt
7272- | Keycode::LAlt
7373- | Keycode::RMeta
7474- | Keycode::LMeta
7575- | Keycode::CapsLock
7676- ) {
7777- continue;
7777+ if !previously_held_keys.contains(key) {
7878+ // Key just pressed, record the time and play initial sound
7979+ key_press_times.insert(*key, std::time::Instant::now());
8080+ play_sound_for_key(*key);
7881 }
7979- if let Some(press_time) = key_press_times.get(key) {
8080- // Only repeat if key has been held longer than initial delay
8181- if now.duration_since(*press_time) >= initial_delay {
8282- play_sound_for_key(*key);
8282+ }
8383+8484+ // Remove timing info for released keys
8585+ key_press_times.retain(|key, _| currently_held_keys.contains(key));
8686+8787+ // Play repeating sounds every 50ms, but only after initial delay
8888+ if last_sound_time.elapsed() >= repeat_interval {
8989+ let now = std::time::Instant::now();
9090+ for key in ¤tly_held_keys {
9191+ if is_modifier_key(key) {
9292+ continue;
9393+ }
9494+ if let Some(press_time) = key_press_times.get(key) {
9595+ // Only repeat if key has been held longer than initial delay
9696+ if now.duration_since(*press_time) >= initial_delay {
9797+ play_sound_for_key(*key);
9898+ }
8399 }
84100 }
101101+ last_sound_time = now;
85102 }
8686- last_sound_time = now;
103103+ } else {
104104+ // Clear key press times when sounds are disabled to avoid stale data
105105+ key_press_times.clear();
87106 }
8810789108 previously_held_keys = currently_held_keys;
···92111 std::thread::sleep(Duration::from_millis(5));
93112 }
94113}
114114+115115+fn is_modifier_key(key: &Keycode) -> bool {
116116+ matches!(
117117+ key,
118118+ Keycode::LShift
119119+ | Keycode::RShift
120120+ | Keycode::LControl
121121+ | Keycode::RControl
122122+ | Keycode::RAlt
123123+ | Keycode::LAlt
124124+ | Keycode::RMeta
125125+ | Keycode::LMeta
126126+ | Keycode::CapsLock
127127+ )
128128+}