forked from joaoabrodrigues/macro-typer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.py
66 lines (55 loc) · 2.29 KB
/
input.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
import itertools
import string
import time
from pynput.keyboard import Key, Controller
# pylint: disable=C0326; it is easier to read column aligned keys
#: The keys used as modifiers; the first value in each tuple is the
#: base modifier to use for subsequent modifiers.
_MODIFIER_KEYS = (
(Key.alt_gr, (Key.alt_gr.value,)),
(Key.alt, (Key.alt.value, Key.alt_l.value, Key.alt_r.value)),
(Key.cmd, (Key.cmd.value, Key.cmd_l.value, Key.cmd_r.value)),
(Key.ctrl, (Key.ctrl.value, Key.ctrl_l.value, Key.ctrl_r.value)),
(Key.shift, (Key.shift.value, Key.shift_l.value, Key.shift_r.value)))
#: Normalised modifiers as a mapping from virtual key code to basic modifier.
_NORMAL_MODIFIERS = {
value: key
for combination in _MODIFIER_KEYS
for key, value in zip(
itertools.cycle((combination[0],)),
combination[1])}
#: Control codes to transform into key codes when typing
_CONTROL_CODES = {
'\n': Key.enter,
'\r': Key.enter,
'\t': Key.tab
}
# pylint: enable=C0326
class KeyboardInput:
def type_string(self, args):
time.sleep(args.delay)
self.type(args)
def type(self, args):
keyboard = Controller()
for i, character in enumerate(args.text):
key = _CONTROL_CODES.get(character, character)
try:
if (self.is_alpha_lower(key) and args.uppercase) or self.shift_chars(key):
with keyboard.pressed(Key.shift):
keyboard.press(key)
keyboard.release(key)
elif self.alt_gr_chars(key):
with keyboard.pressed(Key.alt_gr):
keyboard.press(key)
keyboard.release(key)
else:
keyboard.press(key)
keyboard.release(key)
except (ValueError, keyboard.InvalidKeyException):
raise keyboard.InvalidCharacterException(i, character)
def is_alpha_lower(self, ch):
return False if ch in _CONTROL_CODES.values() else ch in string.ascii_lowercase
def shift_chars(self, ch):
return False if ch in _CONTROL_CODES.values() else ch in '"!@#$%*()_+|<>:?}{^`'
def alt_gr_chars(self, ch):
return False if ch in _CONTROL_CODES.values() else ch in '¹²³£¢¬§°ºª'