-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeyboard_Listener.py
134 lines (118 loc) · 3.25 KB
/
Keyboard_Listener.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#Imports
import keyboard
################################################################################
class Keyboard_Listener:
"""
I wanted a keyboard listener that when a key is pressed (and possibly held)
it would trigger one key press event and one only. Same for released. All
listeners I could find would generate infinite key press events if the key
was held. Since that behavior was undesireable I made this one which only
generates a single key press event if the key is pressed and held
"""
############################################################################
def __init__(self):
"""
PURPOSE: creates a new Keyboard_Listener
ARGS: none
RETURNS: new instance of a Keyboard_Listener
NOTES:
"""
self.key_states = [False] * 255
self.press_cb = None
self.release_cb = None
############################################################################
def __del__(self):
"""
PURPOSE: performs any necessary cleanup
ARGS: none
RETURNS: none
NOTES:
"""
self.stop()
############################################################################
def start(self):
"""
PURPOSE: starts the keyboard listener
ARGS: none
RETURNS: none
NOTES:
"""
keyboard.hook(self.on_key_event)
############################################################################
def stop(self):
"""
PURPOSE: stops the keyboard listener
ARGS: none
RETURNS: none
NOTES:
"""
keyboard.unhook_all()
############################################################################
def on_key_event(self, e):
"""
PURPOSE: keyboard module calls this on a key press event and this
handles that event
ARGS:
e (KeyboardEvent): the keyboard event
RETURNS: none
NOTES:
"""
if len(e.name) == 1:
code = ord(e.name)
elif e.name == 'up':
code = 24
elif e.name == 'down':
code = 25
elif e.name == 'right':
code = 26
elif e.name == 'left':
code = 27
else:
return
if e.event_type == 'down':
if not self.key_states[code]:
self.key_states[code] = True
if self.press_cb:
self.press_cb(code)
elif e.event_type == 'up':
self.key_states[code] = False
if self.release_cb:
self.release_cb(code)
############################################################################
def set_press_cb(self, cb):
"""
PURPOSE: sets the callback function for when a key is pressed
ARGS:
cb (function): callback function that takes in scan code
RETURNS: none
NOTES:
"""
self.press_cb = cb
############################################################################
def set_release_cb(self, cb):
"""
PURPOSE: sets the callback function for when a key is released
ARGS:
cb (function): callback function that takes in scan code
RETURNS: none
NOTES:
"""
self.release_cb = cb
############################################################################
################################################################################
if __name__ == "__main__":
import time
def key_pressed(scan_code):
print("Pressed %d" % scan_code)
def key_released(scan_code):
print("Released %d" % scan_code)
kl = Keyboard_Listener()
kl.set_press_cb(key_pressed)
kl.set_release_cb(key_released)
kl.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt as e:
pass
kl.stop()