-
Notifications
You must be signed in to change notification settings - Fork 4
/
autogui.py
executable file
·272 lines (247 loc) · 7.76 KB
/
autogui.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
import pyautogui, time, sys, os, win32api, win32gui, win32con, datetime, pyHook, pythoncom
from optparse import OptionParser
'''
Python Automated Actions Script by Ian Mckay
Version 0.1 - 20151217
'''
pyautogui.PAUSE = 0
pyautogui.FAILSAFE = True
main_thread_id = win32api.GetCurrentThreadId()
events = []
recording = False
def OnMouseEvent(event):
global events
global recording
if (event.Message!=512): # 512 is mouse move
'''
print('MessageName:',event.MessageName)
print('Message:',event.Message)
print('Time:',event.Time)
print('Window:',event.Window)
print('WindowName:',event.WindowName)
print('Position:',event.Position)
print('Wheel:',event.Wheel)
print('Injected:',event.Injected)
print('---')
'''
if (recording==True):
events.append([event.Position[1],event.Position[0],event.Message,event.Time,"2"])
return True
def OnKeyboardEvent(event):
global hm
global events
global recording
global starttime
global main_thread_id
'''
print('MessageName:',event.MessageName)
print('Message:',event.Message)
print('Time:',event.Time)
print('Window:',event.Window)
print('WindowName:',event.WindowName)
print('Ascii:', event.Ascii, chr(event.Ascii))
print('Key:', event.Key)
print('KeyID:', event.KeyID)
print('ScanCode:', event.ScanCode)
print('Extended:', event.Extended)
print('Injected:', event.Injected)
print('Alt', event.Alt)
print('Transition', event.Transition)
print('---')
'''
if (recording==True):
if (event.Key=="End"):
hm.UnhookKeyboard()
hm.UnhookMouse()
win32api.PostThreadMessage(main_thread_id, win32con.WM_QUIT, 0, 0);
events.append(["0","0","1",event.Time,"0"])
print("Ended recording")
print('\a')
return False
else:
events.append([event.Extended,event.KeyID,event.Message,event.Time,"1"])
if (recording==False):
if (event.Key=="Home" and event.Message==257):
starttime = datetime.datetime.now().time()
recording = True
print("Started recording")
print('\a')
events.append(["0","0","0",event.Time,"0"])
return False
return True
def record():
global hm
print("Hooking now...")
hm = pyHook.HookManager()
hm.MouseAll = OnMouseEvent
hm.KeyAll = OnKeyboardEvent
hm.HookMouse()
hm.HookKeyboard()
print("Hooked")
pythoncom.PumpMessages()
print("Exporting...")
f = open('recording.txt', 'w+')
for event in events:
f.write(str(event.pop()) + ',' + str(event.pop()) + ',' + str(event.pop()) + ',' + str(event.pop()) + ',' + str(event.pop()) + '\n')
f.close()
print("Ending...")
def play():
print("Starting in 2 secs...")
time.sleep(2)
lasttime=False
with open('recording.txt') as fp:
for line in fp:
elements = line.split(',')
eventdata2 = int(elements.pop().replace('\n',''))
eventdata1 = int(elements.pop())
eventsubtype = int(elements.pop())
eventtime = int(elements.pop())
eventtype = int(elements.pop())
if (lasttime==False):
if (eventtype==0 and eventsubtype==0):
lasttime=eventtime
else:
print("Source data error! (eventtype=" + str(eventtype) + ", eventsubtype=" + str(eventsubtype) + ")")
sys.exit(1)
elif (eventtype==1):
time.sleep(max((eventtime-lasttime)/1000,0.02)) # At least 20ms between everything
if (eventdata1>32 and eventdata1<127 and eventdata2==0):
key = chr(eventdata1).lower()
elif (eventdata1==91 and eventdata2==1):
key = "winleft"
elif (eventdata1==9 and eventdata2==0):
key = "tab"
elif (eventdata1==20 and eventdata2==0):
key = "capslock"
elif (eventdata1==160 and eventdata2==0):
key = "shiftleft"
elif (eventdata1==162 and eventdata2==0):
key = "ctrlleft"
elif (eventdata1==164 and eventdata2==0):
key = "altleft"
elif (eventdata1==32 and eventdata2==0):
key = "space"
elif (eventdata1==165 and eventdata2==1):
key = "altright"
elif (eventdata1==163 and eventdata2==1):
key = "ctrlright"
elif (eventdata1==37 and eventdata2==1):
key = "left"
elif (eventdata1==40 and eventdata2==1):
key = "down"
elif (eventdata1==39 and eventdata2==1):
key = "right"
elif (eventdata1==161 and eventdata2==1):
key = "shiftright"
elif (eventdata1==38 and eventdata2==1):
key = "up"
elif (eventdata1==34 and eventdata2==1):
key = "pgdn"
elif (eventdata1==33 and eventdata2==1):
key = "pgup"
elif (eventdata1==8 and eventdata2==0):
key = "backspace"
elif (eventdata1==44 and eventdata2==1):
key = "printscreen"
elif (eventdata1==46 and eventdata2==1):
key = "delete"
elif (eventdata1==27 and eventdata2==0):
key = "esc"
elif (eventdata1==13 and eventdata2==0):
key = "enter"
elif (eventdata1==112 and eventdata2==0):
key = "f1"
elif (eventdata1==113 and eventdata2==0):
key = "f2"
elif (eventdata1==114 and eventdata2==0):
key = "f3"
elif (eventdata1==115 and eventdata2==0):
key = "f4"
elif (eventdata1==116 and eventdata2==0):
key = "f5"
elif (eventdata1==117 and eventdata2==0):
key = "f6"
elif (eventdata1==118 and eventdata2==0):
key = "f7"
elif (eventdata1==119 and eventdata2==0):
key = "f8"
elif (eventdata1==120 and eventdata2==0):
key = "f9"
elif (eventdata1==121 and eventdata2==0):
key = "f10"
elif (eventdata1==122 and eventdata2==0):
key = "f11"
elif (eventdata1==123 and eventdata2==0):
key = "f12"
else:
print("Skipping unknown keycode: " + str(eventdata1))
key = False
if (eventsubtype==256 or eventsubtype==260): # I think 260 is a "virtual keystroke"
if (key!=False):
pyautogui.keyDown(key)
elif (eventsubtype==257):
if (key!=False):
pyautogui.keyUp(key)
else:
print("Bad keyboard subtype!")
sys.exit(1)
lasttime=eventtime
elif (eventtype==2):
time.sleep(max((eventtime-lasttime)/1000,0.02)) # At least 20ms between everything
if (eventsubtype==513):
pyautogui.mouseDown(x=eventdata1, y=eventdata2, button='left')
elif (eventsubtype==514):
pyautogui.mouseUp(x=eventdata1, y=eventdata2, button='left')
else:
print("Bad mouse subtype!")
sys.exit(1)
lasttime=eventtime
elif (eventtype==0 and eventsubtype==1):
print("Done.")
sys.exit(0)
else:
print("Bad source major type!")
sys.exit(1)
print("Done playing")
print('\a')
def main():
usage = "usage: %prog [options]"
parser = OptionParser(usage=usage)
parser.add_option("-r", "--record", action="store_true", dest="do_record", default=False, help="record a session of input")
parser.add_option("-p", "--play", action="store_true", dest="do_play", default=False, help="play a session of input")
(options, args) = parser.parse_args()
if (options.do_record==False and options.do_play==False):
parser.print_help()
sys.exit(0)
if (options.do_record==True):
record()
if (options.do_play==True):
play()
if __name__ == "__main__":
main()
'''
PLAY AREA
w=win32gui
title=w.GetWindowText(w.GetForegroundWindow())
im = None
def capture():
global im
#pyautogui.click(1200, 500)
#pyautogui.typewrite('About to close window!')
#time.sleep(2)
#pyautogui.hotkey('alt', 'f4')
#pyautogui.screenshot()
posx, posy = pyautogui.position()
print(str(posx) + "," + str(posy))
im = pyautogui.screenshot('img.png',region=(posx-20,posy-20,40,40))
def replay():
global im
loc = pyautogui.locateOnScreen(im)
locx, locy = pyautogui.center(loc)
pyautogui.click(locx, locy)
#capture()
#replay()
def post_keys(hwnd, i):
win32api.SendMessage(hwnd, win32con.WM_KEYDOWN, i, 0)
win32api.SendMessage(hwnd, win32con.WM_KEYUP, i, 0)
'''