/
macros_recorder.py
294 lines (253 loc) · 9.96 KB
/
macros_recorder.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
import json
import time
from pynput import keyboard, mouse
from screeninfo import get_monitors
from threading import Event
from find_image_on_screen import find_image_on_screen
class MacrosRecorder:
'''Macros recorder'''
def __init__(self):
# Bool variables for listening and recording
self.is_listening = False
self.is_recording = False
# Keyboard and mouse listeners
self.keyboard_listener = None
self.mouse_listener = None
# Sequence of keyboard and mouse actions
self.macro = []
# A lot of currently pressed keys
self.pressed_keys = set()
# Event for block until wait next action
self.wait_event = Event()
# Last action
self.last_action = None
def _action_distributor(self, device, action, **kwargs):
'''Distributes the action depending on the states'''
if action not in ['press', 'release', 'move', 'scroll']:
raise ValueError('Wrong action name.')
if device == 'keyboard':
action_dict = {
'device': device,
'action': action,
'key': kwargs['key']
}
elif device == 'mouse':
action_dict = {
'device': device,
'action': action,
'x': kwargs['x'],
'y': kwargs['y']
}
if action == 'press' or action == 'release':
action_dict['button'] = kwargs['button']
if action == 'scroll':
action_dict['dx'] = kwargs['dx']
action_dict['dy'] = kwargs['dy']
else:
raise ValueError('Wrong device name.')
self.last_action = action_dict
if self.is_recording:
self.macro.append(action_dict)
# Call event
self.wait_event.set()
self.wait_event.clear()
def wait_action(self):
'''Wait next action'''
# Wait event
self.wait_event.wait()
return self.last_action
def remove(self, index):
'''Remote action from macro by index'''
del self.macro[index]
def clear(self):
'''Clearing macro'''
self.macro = []
def insert(self, other_macro_record, index=-1):
'''Inserting in macro another macro'''
if self.length() <= index < 0: raise ValueError('Index incorrect.')
self.macro[index:index] = other_macro_record.macro
def length(self):
'''Getting count of actions on macro'''
return len(self.macro)
def swap(self, first, second):
'''Swapping two actions in macro by index'''
self.macro[first], self.macro[second] = self.macro[second], self.macro[first]
# vvvvvvvv mouse vvvvvvvv
def _on_move_mouse(self, x, y):
'''Function that activated on mouse move'''
pass
def _on_click_mouse(self, x, y, button, is_pressed):
'''Function that activated on mouse click'''
self._action_distributor(
'mouse',
'press' if is_pressed else 'release',
x=x,
y=y,
button=button
)
def _on_scroll_mouse(self, x, y, dx, dy):
'''Function that activated on mouse scroll'''
self._action_distributor(
'mouse',
'scroll',
x=x,
y=y,
dx=dx,
dy=dy
)
# vvvvvvvv keyboard vvvvvvvv
def _on_press_keyboard(self, key):
'''Function that activated on keyboar key press'''
if key not in self.pressed_keys:
self._action_distributor(
'keyboard',
'press',
key=key
)
self.pressed_keys.add(key)
def _on_release_keyboard(self, key):
'''Function that activated on keyboar key release'''
self._action_distributor(
'keyboard',
'release',
key=key
)
self.pressed_keys.discard(key)
# vvvvvvvv control macro vvvvvvvv
def start_listen(self):
'''Starting action listen'''
assert not self.is_listening, 'Listen already start'
self.keyboard_listener = keyboard.Listener(
on_press=self._on_press_keyboard,
on_release=self._on_release_keyboard
)
self.mouse_listener = mouse.Listener(
on_move=self._on_move_mouse,
on_click=self._on_click_mouse,
on_scroll=self._on_scroll_mouse
)
self.is_listening = True
self.is_recording = False
self.pressed_keys.clear()
self.keyboard_listener.start()
self.mouse_listener.start()
def stop_listen(self):
'''Stopping action listen'''
if self.is_listening:
self.is_listening = False
self.is_recording = False
self.mouse_listener.stop()
self.keyboard_listener.stop()
def start_record(self):
'''Starting macro recording'''
assert self.is_listening, 'Listen not start'
self.is_recording = True
def stop_record(self):
'''Stopping macro recording'''
assert self.is_listening, 'Listen not start'
self.is_recording = False
def run(self):
'''Running macro'''
keyboard_controller = keyboard.Controller()
mouse_controller = mouse.Controller()
for m in self.macro:
time.sleep(0.1)
# Keyboard actions
if m['device'] == 'keyboard':
# Pressing keyboard key
if m['action'] == 'press':
keyboard_controller.press(m['key'])
# Releasing keyboard key
elif m['action'] == 'release':
keyboard_controller.release(m['key'])
# Mouse actions
if m['device'] == 'mouse':
# Moving mouse point
# the move function in mouse controller
# moves the pointer relative to current position
if 'img_hover' in m:
x, y = find_image_on_screen(m['img_hover'])
else:
x, y = m['x'], m['y']
mouse_controller.move(
x - mouse_controller.position[0],
y - mouse_controller.position[1]
)
# Mouse scrolling
if m['action'] == 'scroll':
mouse_controller.scroll(m['dx'], m['dy'])
# Pressing mouse button
elif m['action'] == 'press':
mouse_controller.press(m['button'])
# Releasing mouse button
elif m['action'] == 'release':
mouse_controller.release(m['button'])
# vvvvvvvv export macro vvvvvvvv
@staticmethod
def _get_resolution():
'''Getting resolution of primary monitor'''
for m in get_monitors():
if m.is_primary:
return {'width': m.width, 'height': m.height}
@staticmethod
def _to_relative_coord(x, y):
'''Getting relative mouse pointer coordinates from absolute'''
resolution = MacrosRecorder._get_resolution()
return x / resolution['width'], y / resolution['height']
@staticmethod
def _to_absolute_coord(x, y):
'''Getting absolute mouse pointer coordinates from relative'''
resolution = MacrosRecorder._get_resolution()
return round(x * resolution['width']), round(y * resolution['height'])
def to_json(self):
'''Converting macro to JSON format'''
def convert(m):
'''Convert macro action to JSON element'''
if 'key' in m:
try:
m['key'] = m['key'].char
except AttributeError:
m['key'] = str(m['key']).replace('Key.', '')
elif 'button' in m:
m['button'] = str(m['button']).replace('Button.', '')
if 'x' in m and 'y' in m:
m['x'], m['y'] = MacrosRecorder._to_relative_coord(m['x'], m['y'])
return m
return json.dumps([convert(t) for t in self.macro], indent=4)
def from_json(self, json_str):
'''Getting macro from JSON format'''
self.clear()
def convert(m):
'''Convert JSON element to macro action'''
if 'key' in m:
if len(m['key']) == 1:
m['key'] = keyboard.KeyCode.from_char(m['key'])
else:
m['key'] = keyboard.Key[m['key']]
elif 'button' in m:
m['button'] = mouse.Button[m['button']]
if 'x' in m and 'y' in m:
m['x'], m['y'] = MacrosRecorder._to_absolute_coord(m['x'], m['y'])
return m
for m in json.loads(json_str):
self.macro.append(convert(m))
def get_action(self, index):
'''Getting action in macro in string format'''
if self.macro[index]['device'] == 'keyboard':
if len(str(self.macro[index]["key"])) <= 3:
return f'{self.macro[index]["action"]} {self.macro[index]["key"]}'
else:
return f'{self.macro[index]["action"]} {str(self.macro[index]["key"]).replace("Key.", "").upper()}'
elif self.macro[index]['device'] == 'mouse':
return_string = f'{self.macro[index]["action"]}'
if self.macro[index]['action'] == 'scroll':
dx_direction = {-1: ' left', 0: '', 1: ' right'}
dy_direction = {-1: ' down', 0: '', 1: ' up'}
return_string += f'{dx_direction[self.macro[index]["dx"]]}'\
f'{dy_direction[self.macro[index]["dy"]]};'
elif (self.macro[index]['action'] == 'press' or
self.macro[index]['action'] == 'release'):
return_string += f' {str(self.macro[index]["button"]).replace("Button.", "").upper()} mouse button;'
return_string += f' x: {self.macro[index]["x"]}'\
f' y: {self.macro[index]["y"]}'
return return_string