-
Notifications
You must be signed in to change notification settings - Fork 0
/
recordAndPlay.py
184 lines (164 loc) · 6.34 KB
/
recordAndPlay.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
import win32api
import win32con
import time
import pyautogui
import random
def writer(click):
x, y = pyautogui.position()
print('click recorded at: ' + str(x) + ', ' + str(y))
time.sleep(0.1)
with open(cordFile + '.txt', 'a') as f:
f.write(str(click) + ' ' + str(x) + ' ' + str(y) + ' 1\n')
time.sleep(0.1)
def recorder(action):
print('program ready to record | press Shift to STOP')
# get state from virtual-key-codes
while action:
if (win32api.GetKeyState(0x01)) < 0:
writer(1)
elif (win32api.GetKeyState(0x02)) < 0:
writer(2)
elif (win32api.GetKeyState(0x10)) < 0:
print('program stopped')
action = False
def realTimer(action):
print('program ready to record | press Shift to STOP')
# get state from virtual-key-codes
rTime = time.time()
print(rTime)
while action:
if (win32api.GetKeyState(0x01)) < 0:
x, y = pyautogui.position()
pTime = int(time.time() - rTime)
print('click recorded at: ' + str(x) + ', ' + str(y))
time.sleep(0.1)
with open(cordFile + '.txt', 'a') as f:
f.write('1 ' + str(x) + ' ' + str(y) +
' ' + str(pTime) + ' 1\n')
time.sleep(0.1)
elif (win32api.GetKeyState(0x02)) < 0:
x, y = pyautogui.position()
pTime = int(time.time() - rTime)
print('click recorded at: ' + str(x) + ', ' + str(y))
time.sleep(0.1)
with open(cordFile + '.txt', 'a') as f:
f.write('2 ' + str(x) + ' ' + str(y) +
' ' + str(pTime) + ' 1\n')
time.sleep(0.1)
elif (win32api.GetKeyState(0x10)) < 0:
print('program stopped')
break
def Rec():
# delete the file if it exists
try:
open(cordFile + '.txt', 'w').close()
except:
pass
print(pyautogui.size()) # print screen size
print('mode to select : [1] speed, [2] time-range, [3] real-time')
mode = input('select mode : ')
loop = input('how many loop : ')
if mode == '1':
print('speed mode selected')
with open(cordFile + '.txt', 'a') as f:
f.write('1 0 0 ' + loop + '\n')
recorder(True)
elif mode == '2':
print('time range mode selected')
sTime = input('start range (s) :')
eTime = input('end range (s) :')
timeRange = sTime + ' ' + eTime
print('time range : ' + timeRange)
with open(cordFile + '.txt', 'a') as f:
f.write('2 ' + timeRange + ' ' + loop + '\n')
recorder(True)
elif mode == '3':
print('real time mode selected')
with open(cordFile + '.txt', 'a') as f:
f.write('3 0 0 ' + loop + '\n')
realTimer(True)
else:
print('error')
exit()
def rightCord(xCord=None, yCord=None, pressTimes=1):
pyautogui.rightClick(xCord, yCord, pressTimes)
def clickCord(xCord=None, yCord=None, pressTimes=1):
pyautogui.click(xCord, yCord, pressTimes)
def moveCord(xCord=None, yCord=None, delay=0.1):
pyautogui.moveTo(xCord, yCord, delay)
# read cordFile and loop through each line
def Play():
with open(cordFile + '.txt') as f:
mode, sRange, eRange, loop = map(int, f.readline().split(' '))
if mode == 1:
for _ in range(loop):
print('loop : ' + str(_+1))
with open(cordFile + '.txt') as f:
for line in f.readlines()[1:]:
# print('hi')
which, xCord, yCord, pressTimes = map(int, line.split())
if which == 1:
clickCord(xCord, yCord, pressTimes)
elif which == 2:
rightCord(xCord, yCord, pressTimes)
elif mode == 2:
for _ in range(loop):
print('loop : ' + str(_+1))
print(sRange, eRange)
# interval random float from sRange to eRange
with open(cordFile + '.txt') as f:
for line in f.readlines()[1:]:
interval = random.uniform(sRange, eRange)
print(interval)
which, xCord, yCord, pressTimes = map(int, line.split())
moveCord(xCord, yCord, interval)
if which == 1:
clickCord(xCord, yCord, pressTimes)
elif which == 2:
rightCord(xCord, yCord, pressTimes)
elif mode == 3:
for _ in range(loop):
print('loop : ' + str(_+1))
reminder = 0
with open(cordFile + '.txt') as f:
for line in f.readlines()[1:]:
which, xCord, yCord, interval, pressTimes = map(
int, line.split())
# 0.6 seconds is the default prefix time
moveCord(xCord, yCord, interval - reminder - 0.6)
if which == 1:
clickCord(xCord, yCord, pressTimes)
elif which == 2:
rightCord(xCord, yCord, pressTimes)
reminder = interval
else:
print('mode not found')
def main():
# by marvin zhong at github
print('Welcome to the Mouse Replayer by Marvin Zhong at github')
while True:
print(
'mode to select\t: [1] to record mouse event, [2] to replay your recorded mouse event')
mode = input('select mode\t: ')
if mode == '1':
Rec()
elif mode == '2':
timeSecs = int(input('program will run after (s) : '))
startTime = time.time()
# for timeSecs make countdown
while time.time() - startTime < timeSecs:
print(timeSecs - int(time.time() - startTime))
time.sleep(1)
Play()
endTime = int(time.time() - startTime)
print('program runtime : ', endTime, 's')
else:
print('error')
exit()
print('do you want to close the program? [y/n]')
close = input('close? : ')
if close == 'y':
break
if __name__ == '__main__':
cordFile = 'mousePosition'
main()