forked from george-jensen/record-and-play-pynput
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplay.py
66 lines (52 loc) · 3.23 KB
/
play.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
from pynput.mouse import Button, Controller as MouseController
from pynput.keyboard import Key, Controller as KeyboardController
import time
import json
import sys
n = len(sys.argv)
if n < 3:
exit("Takes two arguments - name of recording to play and number of times to play it")
if n > 3:
exit("Only takes two argument - name of recording to play and number of times to play it")
if n == 3:
name_of_recording = "data/" + str(sys.argv[1]) +'.txt'
number_of_plays = int(sys.argv[2])
with open(name_of_recording) as json_file:
data = json.load(json_file)
special_keys = {"Key.shift": Key.shift, "Key.tab": Key.tab, "Key.caps_lock": Key.caps_lock, "Key.ctrl": Key.ctrl, "Key.alt": Key.alt, "Key.cmd": Key.cmd, "Key.cmd_r": Key.cmd_r, "Key.alt_r": Key.alt_r, "Key.ctrl_r": Key.ctrl_r, "Key.shift_r": Key.shift_r, "Key.enter": Key.enter, "Key.backspace": Key.backspace, "Key.f19": Key.f19, "Key.f18": Key.f18, "Key.f17": Key.f17, "Key.f16": Key.f16, "Key.f15": Key.f15, "Key.f14": Key.f14, "Key.f13": Key.f13, "Key.media_volume_up": Key.media_volume_up, "Key.media_volume_down": Key.media_volume_down, "Key.media_volume_mute": Key.media_volume_mute, "Key.media_play_pause": Key.media_play_pause, "Key.f6": Key.f6, "Key.f5": Key.f5, "Key.right": Key.right, "Key.down": Key.down, "Key.left": Key.left, "Key.up": Key.up, "Key.page_up": Key.page_up, "Key.page_down": Key.page_down, "Key.home": Key.home, "Key.end": Key.end, "Key.delete": Key.delete, "Key.space": Key.space}
mouse = MouseController()
keyboard = KeyboardController()
for loop in range(number_of_plays):
for index, obj in enumerate(data):
action, _time= obj['action'], obj['_time']
try:
next_movement = data[index+1]['_time']
pause_time = next_movement - _time
except IndexError as e:
pause_time = 1
if action == "pressed_key" or action == "released_key":
key = obj['key'] if 'Key.' not in obj['key'] else special_keys[obj['key']]
print("action: {0}, time: {1}, key: {2}".format(action, _time, str(key)))
if action == "pressed_key":
keyboard.press(key)
else:
keyboard.release(key)
time.sleep(pause_time)
else:
move_for_scroll = True
x, y = obj['x'], obj['y']
if action == "scroll" and index > 0:
if x == data[index - 1]['x'] and y == data[index - 1]['y']:
move_for_scroll = False
print("x: {0}, y: {1}, action: {2}, time: {3}".format(x, y, action, _time))
mouse.position = (x, y)
if action == "pressed" or action == "released" or action == "scroll" and move_for_scroll == True:
time.sleep(0.1)
if action == "pressed":
mouse.press(Button.left if obj['button'] == "Button.left" else Button.right)
elif action == "released":
mouse.release(Button.left if obj['button'] == "Button.left" else Button.right)
elif action == "scroll":
horizontal_direction, vertical_direction = obj['horizontal_direction'], obj['vertical_direction']
mouse.scroll(horizontal_direction, vertical_direction)
time.sleep(pause_time)