-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
66 lines (49 loc) · 1.65 KB
/
app.py
File metadata and controls
66 lines (49 loc) · 1.65 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
65
66
import pygame
import sys
import os
import time
from pynput import keyboard
if getattr(sys, 'frozen', False): workspace = os.path.dirname(sys.executable) # In .exe
elif __file__: workspace = os.path.dirname(os.path.abspath(__file__)) # In .py script
sounds = os.path.join(workspace, 'sounds')
pygame.mixer.init()
sound_keyup = pygame.mixer.Sound(os.path.join(sounds,'keyup.mp3'))
sound_keydown = pygame.mixer.Sound(os.path.join(sounds,'keydown.mp3'))
sound_returnup = pygame.mixer.Sound(os.path.join(sounds,'carriage-return-start.mp3'))
sound_returndown = pygame.mixer.Sound(os.path.join(sounds,'carriage-return-end.mp3'))
sound_spacebar = pygame.mixer.Sound(os.path.join(sounds,'spacebar.mp3'))
sound_bell = pygame.mixer.Sound(os.path.join(sounds,'bell.mp3'))
pressed_list = []
def handle_release(key):
Key = str(key)
if Key in pressed_list:
pressed_list.remove(Key)
print(f"{Key} released")
# Key event
if Key == "Key.enter":
sound_bell.play()
# Wait for sound to stop
while pygame.mixer.get_busy():
time.sleep(0.1)
sound_returnup.play()
else:
sound_keyup.play()
def handle_press(key):
Key = str(key)
if Key in pressed_list:
return
else:
pressed_list.append(Key)
print(f"{Key} pressed")
# Key event
if Key == "Key.space":
sound_spacebar.play()
elif Key == "Key.backspace":
sound_spacebar.play()
elif Key == "Key.enter":
sound_returndown.play()
else:
sound_keydown.play()
# Listen for keypress
with keyboard.Listener(on_release = handle_release, on_press=handle_press) as listener:
listener.join()