-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython-key-automation.py
419 lines (289 loc) · 11.9 KB
/
python-key-automation.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
import winput
import time
import random
from datetime import datetime
import mouse
import keyboard
global userchoice
userchoice = 0
global sessionname
sessionname = 'Unknown-session'
global previoustimestamp
global firsttime
firsttime = 0
global status
status = False
global counter1
counter1 = 0
global counter2
counter2 = 0
global counter3
counter3 = 0
global counter4
counter4 = 0
global previousvk
previousvk = '0231231'
##mouuse is a bit inconsistent
##def mouse_callback( event ):
##
## this line not in code#if str(event.action) == '513':
## if event.action == winput.WM_LBUTTONDOWN:
## now = datetime.now()
## eventpos = str(event.position)
## date_time = now.strftime("%Y-%m-%d %H:%M:%S.%f")
## print("LMB press at {}".format( eventpos ))
## SendKeyPressData('LMB',eventpos,date_time)
## print('')
## hooknunhookmouse()
##
##
## if event.action == winput.WM_RBUTTONDOWN:
## now = datetime.now()
## eventpos = str(event.position)
## date_time = now.strftime("%Y-%m-%d %H:%M:%S.%f")
## print("RMB press at {}".format( eventpos ))
## SendKeyPressData('RMB',eventpos,date_time)
## print('')
## hooknunhookmouse()
##def hooknunhookmouse():
## winput.unhook_mouse()
## winput.hook_mouse( mouse_callback )
## #winput.wait_messages()
def keyboard_callback( event ):
global counter3
global counter4
global previousvk
if str(event.action) == '256' and event.vkCode != winput.VK_F1 and event.vkCode != winput.VK_F2 and event.vkCode != winput.VK_SHIFT and event.vkCode != winput.VK_TAB and event.vkCode != winput.VK_MENU and event.vkCode != winput.VK_CONTROL and event.vkCode != winput.VK_CAPITAL:
eventvk = str(event.vkCode)
print(str(event.vkCode))
now = datetime.now()
date_time = now.strftime("%Y-%m-%d %H:%M:%S.%f")
SendKeyPressData(str(event.vkCode),'',date_time)
if event.vkCode == winput.VK_F1:
if counter4 == 0:
print('F1 was pressed the application will pause')
counter4 = 1
winput.unhook_mouse()
winput.unhook_keyboard()
input('To continue press the enter key..')
#winput.hook_mouse( mouse_callback )
winput.hook_keyboard( keyboard_callback )
# enter message loop
winput.wait_messages()
elif counter4 == 1:
counter4 = 0
#to stop keyshare press F2 Key
if event.vkCode == winput.VK_F2 : # quit on pressing Numlock. make it later in a way that it can be switched back on
print('F2 key was pressed. Pythonmation has stopped')
winput.stop()
def SendKeyPressData(keyname,position,timestamp):
#write in a log file
global filename
global previoustimestamp
global firsttime
global sessionname
global firsttimezerostamp
global d1
#firsttimezerostamp = datetime.now()
#firsttimezerostamp = firsttimezerostamp.strftime("%Y-%m-%d %H:%M:%S.%f")
#d1 = firsttimezerostamp.strptime(str(firsttimezerostamp), "%Y-%m-%d %H:%M:%S.%f")
#d1 = datetime.strptime(previoustimestamp, "%Y-%m-%d %H:%M:%S.%f")
if firsttime == 0:
now = datetime.now()
date_time = now.strftime("%m-%d-%Y, %Hh%Mm%Ss")
filename = 'Pythonmation - ' + sessionname + ' - ' + date_time + '.txt'
with open(filename,"w") as f:
f.write(str(keyname) + ',' + position + ',' + timestamp + ',' + '0:00:00.00' + ',' + '\n')
firsttimezerostamp = datetime.now()
#firsttimezerostamp = firsttimezerostamp.strftime("%Y-%m-%d %H:%M:%S.%f")
d1 = firsttimezerostamp.strptime(str(firsttimezerostamp), "%Y-%m-%d %H:%M:%S.%f")
firsttime = 1
print('0:00:00.00')
print('')
f.close()
previoustimestamp = timestamp
elif firsttime == 1:
with open(filename,"a") as f:
d2 = datetime.strptime(timestamp, "%Y-%m-%d %H:%M:%S.%f")
timediff = (d2 - d1)
print(str(timediff))
print('')
timediff = str(timediff)
f.write(str(keyname) + ',' + position + ',' + timestamp + ',' + timediff + ',' +'\n')
f.close()
previoustimestamp = timestamp
def Mousemovement():
position = winput.get_mouse_pos()
return [position[0],position[1]]
def GetInput():
global userchoice
global sessionname
sessionname = 'Unknown-session'
userchoice = 0
print('Welcome to pythonmation. here you can automate you mouse and keyboard activity and replay them!')
while(int(userchoice) == 0):
print('To play a previous recording type 1. To make a new recording Type 2')
userchoice = input('')
userchoice = int(userchoice)
if(userchoice != 1):
if(userchoice != 2):
userchoice = 0
if userchoice == 1:
return 1
elif userchoice == 2:
print('Type the session name')
sessionname = input('')
return 2
def countdown(seconds):
if seconds == 10:
secs = ['10','9','8','7','6','5','4','3','2','1','Replay started..']
for i in secs:
print(i)
time.sleep(1)
elif seconds == 5:
secs = ['5','4','3','2','1','Replay started..']
for i in secs:
print(i)
time.sleep(1)
elif seconds == '5rec':
secs = ['5','4','3','2','1','Recording started..']
for i in secs:
print(i)
time.sleep(1)
elif seconds == 3:
secs = ['3','2','1','Replay started..']
for i in secs:
print(i)
time.sleep(1)
def Replayrecording(recordingname,numberoftimes):
global status
global line
line = ''
for i in range(int(numberoftimes)):
print('')
print('Replay number ' + str(i + 1))
print('')
if i == 0:
countdown(10)
else:
countdown(3)
with open(recordingname,"r") as f:
starttime = datetime.now()
line = f.readline()
print('')
while line != '':
keyboard.press_and_release('shift')
linesplit = line.split(',')
if(('LMB' or 'RMB') in linesplit[0]):
if('0:00:00.00' in linesplit[4]):
print(linesplit[4])
print(linesplit[0])
tbposx = linesplit[1].split(',')
posx = tbposx[0][1:]
posy = linesplit[2][1:-1]
winput.set_mouse_pos(int(posx), int(posy))
mousebtn = ''
if 'LMB' in linesplit[0]:
mouse.click(button='left')
if 'RMB' in linesplit[0]:
mouse.click(button='right')
previoustime = datetime.now()
line = f.readline()
else:
currenttime = datetime.now()
while (str(currenttime - starttime)[0:8]) not in linesplit[4]:
currenttime = datetime.now()
#print('not exactly the same' + str(currenttime - previoustime))
#out of while loop
print(linesplit[4])
print(linesplit[0])
tbposx = linesplit[1].split(',')
posx = tbposx[0][1:]
posy = linesplit[2][1:-1]
winput.set_mouse_pos(int(posx), int(posy))
mousebtn = ''
if 'LMB' in linesplit[0]:
mousebtn = 'left'
elif 'RMB' in linesplit[0]:
mousebtn = 'right'
#winput.press_mouse_button(mousebtn)
mouse.click(button=mousebtn)
previoustime = datetime.now()
line = f.readline()
#not mouse click
else:
previoustime = datetime.now()
#currenttime = datetime.now()
if('0:00:00.00' in linesplit[3]):
print(linesplit[3])
print(linesplit[0])
winput.press_key(int(linesplit[0]))
previoustime = datetime.now()
line = f.readline()
else:
currenttime = datetime.now()
while (str(currenttime - starttime)[0:8]) not in linesplit[3]:
currenttime = datetime.now()
#print('not exactly the same' + str(currenttime - previoustime))
print(linesplit[3])
print(linesplit[0])
winput.press_key(int(linesplit[0]))
previoustime = datetime.now()
line = f.readline()
f.close()
endtime = datetime.now()
#print(str(endtime-starttime))
def StartApp():
try:
thechoice = GetInput()
if thechoice == 2:
print('')
countdown('5rec')
print("Press F1 to pause. Press F2 to quit")
# hook input
##winput.hook_mouse( mouse_callback )
winput.hook_keyboard( keyboard_callback )
# enter message loop
winput.wait_messages()
# remove input hook
##winput.unhook_mouse()
winput.unhook_keyboard()
elif thechoice == 1:
print('Enter the full name of the recording file to replay, or choose one from the list below by typing the number (example Num0)')
linecounter = 0
with open("replayhistory.txt","r") as f:
for line in f:
if line != '':
print('Num' + str(linecounter) + ': ' + line[:-1])
linecounter = int(linecounter)
linecounter = linecounter + 1
f.close()
isshortcut = False
recordingname = input('')
recordingnum = '-1'
historyfilelines = ''
if(('Num' in recordingname) or ('num' in recordingname)):
recordingnum = recordingname.replace('Num','')
recordingnum = recordingname.replace('num','')
isshortcut = True
if recordingnum != '-1':
with open("replayhistory.txt","r") as f:
historyfilelines = f.readlines()
f.close()
recordingname = historyfilelines[int(recordingnum)][:-1]
if isshortcut == False:
with open("replayhistory.txt","a") as f:
f.write(recordingname + '\n')
f.close()
print('How many times do you want to replay')
nrofreplays = input('')
nrofreplays = int(nrofreplays)
Replayrecording(recordingname,nrofreplays)
##winput.unhook_mouse()
winput.unhook_keyboard()
except Exception as e:
print('Exception was thrown. ' + str(e))
##winput.unhook_mouse()
winput.unhook_keyboard()
#Program
StartApp()