-
Notifications
You must be signed in to change notification settings - Fork 1
/
hook.py
79 lines (60 loc) · 1.88 KB
/
hook.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
"""
Fixing a bug on windows with key character
+ allowing multiple listeners
"""
from pykeyboard import PyKeyboardEvent
from pymouse import PyMouseEvent
import platform
SYSTEM = platform.system()
CHARACTER_MAP = {
27: "escape",
65: "a",
68: "d",
69: "e",
73: "i",
81: "q",
83: "s",
87: "w",
88: "x",
112: "F1",
113: "F2",
114: "F3",
115: "F4",
}
# there should only be one instance
class KeyboardHook(PyKeyboardEvent):
def __init__(self):
PyKeyboardEvent.__init__(self)
# list of listener functions
self.tapListeners = []
# overwrite default _tap method on windows
def _tap(self, event):
if SYSTEM == "Windows":
keycode = event.KeyID
press_bool = (event.Message in [self.hc.WM_KEYDOWN, self.hc.WM_SYSKEYDOWN])
if keycode in CHARACTER_MAP:
character = CHARACTER_MAP[keycode]
else:
character = "Unknown character, keycode: " + str(keycode)
self.tap(keycode, character, press_bool)
else:
PyKeyboardEvent._tap(self, event)
def tap(self, keycode, character, press):
for listener in self.tapListeners:
listener(keycode, character, press)
# bonus - disable default escape action
def escape(self, event):
pass
def addTapListener(self, listener):
self.tapListeners.append(listener)
# there should only be one instance
class MouseHook(PyMouseEvent):
def __init__(self):
PyMouseEvent.__init__(self)
# list of listener functions
self.moveListeners = []
def move(self, x, y):
for listener in self.moveListeners:
listener(x, y)
def addMoveListener(self, listener):
self.moveListeners.append(listener)