-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
56 lines (46 loc) · 1.3 KB
/
main.py
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
import keyboard
from pygame import mixer
import os
import json
config_path = os.path.join(os.path.dirname(__file__), 'config.json')
try:
with open(config_path, 'r') as f:
config = json.load(f)
except Exception as e:
print(f"Error loading config file: {e}")
exit(1)
sound_path = os.path.join(os.path.dirname(__file__), config['sound']['file'])
mixer.init()
mixer.set_num_channels(config['sound']['num_channels'])
try:
mp3_sound = mixer.Sound(sound_path)
mp3_sound.set_volume(config['sound']['volume'])
except Exception as e:
print(f"Error loading sound file: {e}")
exit(1)
def play_mp3sound(e):
if hasattr(e, 'name') and e.name == 'esc':
return
try:
channel = mixer.find_channel()
if channel:
channel.play(mp3_sound)
except Exception as e:
print(f"Error in play_mp3sound: {e}")
if "all" in config['keys_to_monitor']:
keyboard.on_press(play_mp3sound)
else:
for key in config['keys_to_monitor']:
keyboard.on_press_key(key, play_mp3sound)
print("KeyShots")
print(f"Press 'esc' to exit")
try:
keyboard.wait('esc')
except (KeyboardInterrupt, SystemExit):
print("\nProgram terminated by user")
finally:
try:
keyboard.unhook_all()
mixer.quit()
except Exception as e:
print(e)