-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathhelper.py
More file actions
115 lines (89 loc) · 3.55 KB
/
helper.py
File metadata and controls
115 lines (89 loc) · 3.55 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
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
"""
This code is from streamdeck-ui-gui: https://github.com/streamdeck-linux-gui/streamdeck-linux-gui
"""
from .mappings import _SUPPORTED_KEYS, _SPECIAL_KEYS, _OLD_NUMPAD_KEYS, _OLD_PYNPUT_KEYS, _MODIFIER_KEYS, _KEY_MAPPING, _SHIFT_KEY_MAPPING, _ALL_KEYS_DICT, _MASTER_DICT
from evdev import InputDevice, UInput, list_devices
from evdev import ecodes as e
from loguru import logger as log
import time
import pyclip
def get_valid_key_names() -> list[str]:
"""Returns a list of valid key names."""
key_names = [key for key in _SUPPORTED_KEYS]
key_names.extend(_SPECIAL_KEYS.keys())
key_names.extend(_OLD_NUMPAD_KEYS.keys())
key_names.extend(_OLD_PYNPUT_KEYS.keys())
key_names.extend(_MODIFIER_KEYS.keys())
return sorted(key_names)
def check_caps_lock() -> bool:
"""Returns True if Caps Lock is on, False if it is off, and False if it cannot be determined."""
devices = [InputDevice(path) for path in list_devices()]
for device in devices:
if device.capabilities().get(e.EV_LED):
return e.LED_CAPSL in device.leds()
return False
def keyboard_write(ui: UInput, text: str, delay: float = 0.01):
caps_lock_is_on = check_caps_lock()
has_unicode = any(char not in _KEY_MAPPING for char in text)
if has_unicode:
log.debug(f"Text contains Unicode characters, using clipboard method for entire text")
try:
original_clipboard = pyclip.paste()
except Exception:
original_clipboard = ""
pyclip.copy(text)
ui.write(e.EV_KEY, e.KEY_LEFTCTRL, 1)
ui.write(e.EV_KEY, e.KEY_V, 1)
ui.write(e.EV_KEY, e.KEY_V, 0)
ui.write(e.EV_KEY, e.KEY_LEFTCTRL, 0)
ui.syn()
time.sleep(delay)
try:
pyclip.copy(original_clipboard)
except Exception:
pass
else:
for char in text:
keycode = _KEY_MAPPING[char]
need_shift = False
if char in _SHIFT_KEY_MAPPING:
need_shift = True
if char.isalpha() and caps_lock_is_on:
need_shift = not need_shift
if need_shift:
ui.write(e.EV_KEY, e.KEY_LEFTSHIFT, 1)
ui.write(e.EV_KEY, keycode, 1)
ui.write(e.EV_KEY, keycode, 0)
if need_shift:
ui.write(e.EV_KEY, e.KEY_LEFTSHIFT, 0)
# send keys
ui.syn()
time.sleep(delay)
def parse_key_combination(key_combination: str) -> list[int]:
keys = key_combination.lower().split('+')
parsed_keys = []
for key in keys:
if key in _MASTER_DICT:
parsed_keys.append(_MASTER_DICT[key])
else:
log.error(f"Unsupported key: {key}")
# raise ValueError(f"Unsupported key: {key}")
return parsed_keys
# Function to press and release keys
def press_key_combination(ui: UInput, key_combination: str, delay: float = 0.01):
keycodes = parse_key_combination(key_combination)
# Press each key in the combination
for keycode in keycodes:
ui.write(e.EV_KEY, keycode, 1) # Key down
ui.syn()
# Short delay between press and release
time.sleep(delay)
# Release each key in the combination
for keycode in keycodes:
ui.write(e.EV_KEY, keycode, 0) # Key up
ui.syn()
# text = "Hello, World!"
# from evdev import ecodes
# ui = UInput({ecodes.EV_KEY: range(0, 300),
# ecodes.EV_REL: [ecodes.REL_X, ecodes.REL_Y]}, name="stream-controller-os-plugin")
# keyboard_write(ui, text)