-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyboard_control.py
335 lines (283 loc) · 9.69 KB
/
keyboard_control.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# -*- coding: utf-8 -*-
"""
Module created to simplify the use of mouse.
Maybe it could have problems with X display.
"""
from pynput import keyboard
import re
import subprocess
class KeyboardControl(object):
"""Class that simplify use of keyboard in automation."""
def __init__(self, alt_gr_map=None):
super(KeyboardControl, self).__init__()
self.block = False
self.modifiers = {
'alt': keyboard.Key.alt,
'alt': keyboard.Key.alt_gr,
'alt': keyboard.Key.alt_l,
'alt': keyboard.Key.alt_r,
'cmd': keyboard.Key.cmd,
'cmd': keyboard.Key.cmd_l,
'cmd': keyboard.Key.cmd_r,
'ctrl': keyboard.Key.ctrl,
'ctrl': keyboard.Key.ctrl_l,
'ctrl': keyboard.Key.ctrl_r,
'shift': keyboard.Key.shift,
'shift': keyboard.Key.shift_l,
'shift': keyboard.Key.shift_r
}
self.specials = [
keyboard.Key.enter,
keyboard.Key.backspace,
keyboard.Key.caps_lock,
keyboard.Key.delete,
keyboard.Key.down,
keyboard.Key.end,
keyboard.Key.esc,
keyboard.Key.home,
keyboard.Key.insert,
keyboard.Key.left,
keyboard.Key.media_next,
keyboard.Key.media_play_pause,
keyboard.Key.media_previous,
keyboard.Key.media_volume_down,
keyboard.Key.media_volume_mute,
keyboard.Key.media_volume_up,
keyboard.Key.menu,
keyboard.Key.num_lock,
keyboard.Key.page_down,
keyboard.Key.page_up,
keyboard.Key.pause,
keyboard.Key.print_screen,
keyboard.Key.right,
keyboard.Key.scroll_lock,
keyboard.Key.up,
keyboard.Key.f1,
keyboard.Key.f2,
keyboard.Key.f3,
keyboard.Key.f4,
keyboard.Key.f5,
keyboard.Key.f6,
keyboard.Key.f7,
keyboard.Key.f8,
keyboard.Key.f9,
keyboard.Key.f10,
keyboard.Key.f11,
keyboard.Key.f12,
keyboard.Key.f13,
keyboard.Key.f14,
keyboard.Key.f15,
keyboard.Key.f16,
keyboard.Key.f17,
keyboard.Key.f18,
keyboard.Key.f19,
keyboard.Key.f20,
keyboard.Key.space,
keyboard.Key.tab,
]
if alt_gr_map == None:
"""
PROBLEM: And a big one. In too many layouts,
for this module, AltGr doesn't work like it must,
because it has another code. So, the only
solution that I found, is to create a mapping.
But it depends on the layout. This one would
work for me, but no for other.
"""
self.alt_gr_map = {
'1': '|',
'2': '@',
'3': '·',
'4': '~',
'5': '½',
'6': '¬',
'7': '{',
'8': '[',
'9': ']',
'0': '}',
'"': '\\',
'¿': '¸',
'~': '`',
'[': '¨',
'+': '~',
'{': '^',
'q': '@',
'-': '^',
'<': '|',
'z': '«',
'x': '»'
}
self.block_key = "ctrl+/esc"
self.on_press_f = None
self.on_release_f = None
self.last_modifiers = []
self.alt_gr_keycode = "<65027>"
self.on_caps_lock = False
self.init()
def init(self):
"""
Runs some init functions.
- Inits the keyboard controller.
- Disables CapsLock.
"""
self.controller = keyboard.Controller()
if self.check_lock_keys_state(0):
self.controller.tap(keyboard.Key.caps_lock)
def get_key_str(self, key):
key = key.__str__().split('.')[-1]
key = re.search(r"[^'.+]+", key).group(0)
return key
def check_lock_keys_state(self, lock_key):
"""
Function that checks the state of
Caps Lock, Scroll Lock, Nums Lock.
The parameter lock_key works like:
0: Caps Lock
1: Nums Lock
2: Scroll Lock
IMPORTANT: This will only work in Linux systems.
"""
output = subprocess.Popen(
"xset -q | grep Caps",
shell=True,
stdout=subprocess.PIPE
).stdout.read().decode()
lock_states = re.findall(r"on|off", output)
# Returns on or off for each lock in order
return True if lock_states[lock_key] == 'on' else False
def prepare_keys(self, key):
"""
Formats the string that represents the key.
"""
if key in self.specials:
key = '/' + self.get_key_str(key)
# Escapes the special chars
if key == '/caps_lock':
self.on_caps_lock = not(self.on_caps_lock)
# Have a register of the caps lock
else:
key = self.get_key_str(key)
"""
Declared behaviour: Keyboard layout sublevels will
be ignored. Always that alt_gr is in modifiers, the
respective special character is use. If shift or other
modifier is pressed, it will be ignored.
"""
if "alt_gr" in self.last_modifiers:
if key in self.alt_gr_map:
key = self.alt_gr_map[key]
elif "shift" in self.last_modifiers:
if key.isalpha():
key == key.upper()
elif self.on_caps_lock and key.isalpha():
key = key.upper()
for modifier in self.last_modifiers:
if modifier in ['alt_gr', 'shift']: continue
key = modifier.__str__() + '+' + key
return key
def on_press(self, key):
"""
Function called by the listener to prepare
the key to be passed to the function given
as argument.
Also, this includes the blocking cancel
key combination listening.
"""
if key in self.modifiers:
key = self.get_key_str(key)
# Expressed by default like Key.modifier
if key not in self.last_modifiers:
self.last_modifiers.append(key)
elif self.get_key_str(key) == self.alt_gr_keycode:
self.last_modifiers.append("alt_gr")
else:
key = self.prepare_keys(key)
if self.block and key == self.block_key:
return False
if self.on_press_f != None:
self.on_press_f()
def on_release(self, key):
"""
Function called by the listener to prepare
the key to be passed to the function given
as argument.
"""
if key in self.modifiers:
key = self.get_key_str(key)
if key in self.last_modifiers:
self.last_modifiers.remove(key)
elif self.get_key_str(key) == self.alt_gr_keycode:
self.last_modifiers.remove("alt_gr")
else:
key = self.prepare_keys(key)
if self.on_release_f != None:
self.on_release_f()
def start_listening(
self,
on_press=None,
on_release=None,
blocking=False
):
"""
Function that configures the listening using
arguments to set all properties.
"""
self.on_press_f = on_press
self.on_release_f = on_release
if blocking:
self.block = True
with keyboard.Listener(
on_press=self.on_press,
on_release=self.on_release
) as listener:
listener.join()
else:
listener = keyboard.Listener(
on_press=self.on_press,
on_release=self.on_release
)
listener.start()
# --------------------------------------------------
# ----------------- KEYBOARD CONTROL ---------------
# --------------------------------------------------
def get_key_from_str(self, key_str):
"""
This function return a Key object,
from keyboard.Key, receiving a string
that represents it.
"""
keys = list()
if type(key_str) == list:
for key in key_str:
if key in self.modifiers:
keys.append(self.modifiers[key])
return keys
else:
return self.modifiers[key_str]
def send_keys(self, keys_list=None):
"""
Writes the keys received as text.
- If receives a letter, writes it.
- If receives a combination (modifier+key)
send it all.
- If receives a text, writes it as text.
"""
if keys_list:
for key in keys_list:
if "+" in key and len(key) > 1:
keys = key.split("+")
modifiers = self.get_key_from_str(keys[0:-1])
# Supposing all keys are modifiers except
# last one
with self.controller.pressed(*modifiers):
self.controller.press(keys[-1])
elif key in self.specials:
key_code = keyboard.KeyCode.from_char(key)
self.controller.tap(key_code)
else:
self.controller.type(key)
def main():
keyboard_controller = KeyboardControl()
keyboard_controller.send_keys(['ctrl+shift+v'])
if __name__ == "__main__":
main()