This repository has been archived by the owner on Jan 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
output.py
65 lines (43 loc) · 1.68 KB
/
output.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
from evdev import ecodes
from evdev.uinput import UInput
from key import Action, Combo, Modifier
__author__ = 'zh'
_uinput = UInput()
_pressed_modifier_keys = set()
def update_modifier_key_pressed(key, action):
if key in Modifier.get_all_keys():
if action.is_pressed():
_pressed_modifier_keys.add(key)
else:
_pressed_modifier_keys.discard(key)
def send_sync():
_uinput.syn()
def send_event(event):
_uinput.write_event(event)
send_sync()
def send_key_action(key, action):
print("[{}] {}".format(action.name[:3], key.name))
update_modifier_key_pressed(key, action)
_uinput.write(ecodes.EV_KEY, key, action)
send_sync()
def send_combo(combo):
released_modifiers_keys = []
for modifier in set(Modifier) - combo.modifiers:
for modifier_key in modifier.get_keys():
if modifier_key in _pressed_modifier_keys:
send_key_action(modifier_key, Action.RELEASE)
released_modifiers_keys.append(modifier_key)
pressed_modifier_keys = []
for modifier in combo.modifiers:
if not any(modifier_key in _pressed_modifier_keys for modifier_key in modifier.get_keys()):
modifier_key = modifier.get_key()
send_key_action(modifier_key, Action.PRESS)
pressed_modifier_keys.append(modifier_key)
send_key_action(combo.key, Action.PRESS)
send_key_action(combo.key, Action.RELEASE)
for modifier in reversed(pressed_modifier_keys):
send_key_action(modifier, Action.RELEASE)
for modifier in reversed(released_modifiers_keys):
send_key_action(modifier, Action.PRESS)
def send_key(key):
send_combo(Combo(None, key))