-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhotkey_manager.py
More file actions
64 lines (54 loc) · 2.09 KB
/
hotkey_manager.py
File metadata and controls
64 lines (54 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import keyboard
import json
import os
import threading
class HotkeyManager:
def __init__(self, bindings_file, callback_fn):
self.bindings_file = bindings_file
self.callback_fn = callback_fn # Function to call: fn(sound_path)
self.bindings = {} # { key: sound_name }
self.load()
def load(self):
if os.path.exists(self.bindings_file):
try:
with open(self.bindings_file, 'r') as f:
self.bindings = json.load(f)
except:
self.bindings = {}
self.apply_bindings()
def save(self):
with open(self.bindings_file, 'w') as f:
json.dump(self.bindings, f, indent=2)
def apply_bindings(self):
keyboard.unhook_all()
for key, sound in self.bindings.items():
if key and sound:
try:
# We use a closure to capture the sound variable
keyboard.add_hotkey(key, lambda s=sound: self.callback_fn(s))
except Exception as e:
print(f"Failed to bind {key}: {e}")
def set_binding(self, key, sound_name):
# Remove old binding for this sound if exists (optional, or allow multiple keys?)
# Let's allow 1 key per sound for simplicity in UI
# But wait, the UI maps Key -> Sound.
# If we rebind a key, we overwrite.
# Also check if sound was bound to another key?
# The UI logic in script.js was: keyBindings[code] = file.
self.bindings[key] = sound_name
self.save()
self.apply_bindings()
def remove_binding(self, key):
if key in self.bindings:
del self.bindings[key]
self.save()
self.apply_bindings()
def get_bindings(self):
return self.bindings
def clear_sound_binding(self, sound_name):
# Remove any key bound to this sound
keys_to_remove = [k for k, v in self.bindings.items() if v == sound_name]
for k in keys_to_remove:
del self.bindings[k]
self.save()
self.apply_bindings()