···
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();
42
+
let mut sounds_enabled = true; // Toggle for enabling/disabling sounds
let initial_delay = Duration::from_millis(500); // Wait 500ms before starting to repeat
let repeat_interval = Duration::from_millis(50); // Then repeat every 50ms
···
let currently_held_keys: HashSet<Keycode> = state.query_keymap().into_iter().collect();
49
-
// Track when keys were first pressed
50
-
for key in ¤tly_held_keys {
51
-
if !previously_held_keys.contains(key) {
52
-
// Key just pressed, record the time and play initial sound
53
-
key_press_times.insert(*key, std::time::Instant::now());
54
-
play_sound_for_key(*key);
50
+
// Check for toggle hotkey (Ctrl + Alt + L/R Shift + C)
51
+
let hotkey_combo = [
52
+
[Keycode::LControl, Keycode::RControl], // Either left or right control
53
+
[Keycode::LAlt, Keycode::RAlt], // Either left or right alt
54
+
[Keycode::LShift, Keycode::RShift], // Either left or right shift
55
+
[Keycode::C, Keycode::C], // C key (duplicated for array consistency)
58
-
// Remove timing info for released keys
59
-
key_press_times.retain(|key, _| currently_held_keys.contains(key));
58
+
let check_hotkey = |current: &HashSet<Keycode>, previous: &HashSet<Keycode>| {
59
+
hotkey_combo.iter().all(|key_group| {
62
+
.any(|key| current.contains(key) || previous.contains(key))
61
-
// Play repeating sounds every 50ms, but only after initial delay
62
-
if last_sound_time.elapsed() >= repeat_interval {
63
-
let now = std::time::Instant::now();
66
+
let hotkey_active = check_hotkey(¤tly_held_keys, &previously_held_keys);
67
+
let hotkey_was_active = check_hotkey(&previously_held_keys, &HashSet::new());
69
+
if hotkey_active && !hotkey_was_active {
70
+
sounds_enabled = !sounds_enabled;
73
+
// Only process sound logic if sounds are enabled
75
+
// Track when keys were first pressed
for key in ¤tly_held_keys {
77
+
if !previously_held_keys.contains(key) {
78
+
// Key just pressed, record the time and play initial sound
79
+
key_press_times.insert(*key, std::time::Instant::now());
80
+
play_sound_for_key(*key);
79
-
if let Some(press_time) = key_press_times.get(key) {
80
-
// Only repeat if key has been held longer than initial delay
81
-
if now.duration_since(*press_time) >= initial_delay {
82
-
play_sound_for_key(*key);
84
+
// Remove timing info for released keys
85
+
key_press_times.retain(|key, _| currently_held_keys.contains(key));
87
+
// Play repeating sounds every 50ms, but only after initial delay
88
+
if last_sound_time.elapsed() >= repeat_interval {
89
+
let now = std::time::Instant::now();
90
+
for key in ¤tly_held_keys {
91
+
if is_modifier_key(key) {
94
+
if let Some(press_time) = key_press_times.get(key) {
95
+
// Only repeat if key has been held longer than initial delay
96
+
if now.duration_since(*press_time) >= initial_delay {
97
+
play_sound_for_key(*key);
101
+
last_sound_time = now;
86
-
last_sound_time = now;
104
+
// Clear key press times when sounds are disabled to avoid stale data
105
+
key_press_times.clear();
previously_held_keys = currently_held_keys;
···
std::thread::sleep(Duration::from_millis(5));
115
+
fn is_modifier_key(key: &Keycode) -> bool {
120
+
| Keycode::LControl
121
+
| Keycode::RControl
126
+
| Keycode::CapsLock