-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput_manager.py
More file actions
53 lines (45 loc) · 1.83 KB
/
input_manager.py
File metadata and controls
53 lines (45 loc) · 1.83 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
import keyboard
import mouse
class InputEngine:
def __init__(self, is_active_func):
self.is_active_func = is_active_func
self.hotkeys = {}
mouse.hook(self._mouse_listener)
def register(self, hotkey_str, callback):
parts = hotkey_str.lower().replace(' ', '').split('+')
mouse_buttons = ['left', 'right', 'middle', 'mb4', 'mb5']
is_mouse_bind = any(b in parts for b in mouse_buttons)
if is_mouse_bind:
self.hotkeys[hotkey_str] = {'parts': parts, 'callback': callback}
else:
def secure_callback():
if self.is_active_func():
callback()
for p in parts:
if p in ['ctrl', 'alt', 'shift']:
keyboard.release(p)
try:
keyboard.add_hotkey(hotkey_str, secure_callback, suppress=False)
except:
pass
def _mouse_listener(self, event):
if isinstance(event, mouse.ButtonEvent) and event.event_type == 'down':
if not self.is_active_func():
return
btn = str(event.button).lower()
if btn == 'x': btn = 'mb4'
if btn == 'x2': btn = 'mb5'
for hk_str, data in self.hotkeys.items():
parts = data['parts']
if btn in parts:
mods = [p for p in parts if p in ['ctrl', 'alt', 'shift']]
if all(keyboard.is_pressed(m) for m in mods):
data['callback']()
for m in mods:
keyboard.release(m)
def clear(self):
try:
keyboard.unhook_all_hotkeys()
except AttributeError: pass
except Exception: pass
self.hotkeys.clear()