-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMacro_Recorder.ahk
367 lines (335 loc) · 7.97 KB
/
Macro_Recorder.ahk
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
;https://www.autohotkey.com/boards/viewtopic.php?t=34184
;-----------------------------------
; Macro Recorder v2.1 By FeiYue
;
; Description: This script records the mouse
; and keyboard actions and then plays back.
;
; F1 --> Record(Screen) (CoordMode, Mouse, Screen)
; F2 --> Record(Window) (CoordMode, Mouse, Window)
; F3 --> Stop Record/Play
; F4 --> Play LogFile
; F5 --> Edit LogFile
; F6 --> Pause Record/Play
;
; Note:
; 1. press the Ctrl button individually
; to record the movement of the mouse.
; 2. Shake the mouse on the Pause button,
; you can pause recording or playback.
;-----------------------------------
#NoEnv
SetBatchLines, -1
Thread, NoTimers
CoordMode, ToolTip
SetTitleMatchMode, 2
DetectHiddenWindows, On
;--------------------------
LogFile:=A_Temp . "\~Record.txt"
UsedKeys:="F1,F2,F3,F4,F5,F6"
Play_Title:=RegExReplace(LogFile,".*\\") " ahk_class AutoHotkey"
;--------------------------
Gui, +AlwaysOnTop -Caption +ToolWindow +E0x08000000 +Hwndgui_id
Gui, Margin, 0, 0
Gui, Font, s12
s:="[F1]Record(Screen),[F2]Record(Window),"
. "[F3]Stop,[F4]Play,[F5]Edit,[F6] Pause "
For i,v in StrSplit(s, ",")
{
j:=i=1 ? "":"x+0", j.=InStr(v,"Pause") ? " vPause":""
Gui, Add, Button, %j% gRun, %v%
}
Gui, Add, Button, x+0 w0 Hidden vMyText
Gui, Show, NA y0, Macro Recorder
OnMessage(0x200,"WM_MOUSEMOVE")
;--------------------------
SetTimer, OnTop, 2000
OnTop:
Gui, +AlwaysOnTop
return
Run:
if IsLabel(k:=RegExReplace(RegExReplace(A_GuiControl,".*]"),"\W"))
Goto, %k%
return
WM_MOUSEMOVE() {
static OK_Time
ListLines, Off
if (A_Gui=1) and (A_GuiControl="Pause")
and (t:=A_TickCount)>OK_Time
{
OK_Time:=t+500
Gosub, Pause
}
}
ShowTip(s:="", pos:="y35", color:="Red|00FFFF") {
static bak, idx
if (bak=color "," pos "," s)
return
bak:=color "," pos "," s
SetTimer, ShowTip_ChangeColor, Off
Gui, ShowTip: Destroy
if (s="")
return
; WS_EX_NOACTIVATE:=0x08000000, WS_EX_TRANSPARENT:=0x20
Gui, ShowTip: +LastFound +AlwaysOnTop +ToolWindow -Caption +E0x08000020
Gui, ShowTip: Color, FFFFF0
WinSet, TransColor, FFFFF0 150
Gui, ShowTip: Margin, 10, 5
Gui, ShowTip: Font, Q3 s20 bold
Gui, ShowTip: Add, Text,, %s%
Gui, ShowTip: Show, NA %pos%, ShowTip
SetTimer, ShowTip_ChangeColor, 1000
ShowTip_ChangeColor:
Gui, ShowTip: +AlwaysOnTop
r:=StrSplit(SubStr(bak,1,InStr(bak,",")-1),"|")
Gui, ShowTip: Font, % "Q3 c" r[idx:=Mod(Round(idx),r.length())+1]
GuiControl, ShowTip: Font, Static1
return
}
;============ Hotkey =============
F1::
Suspend, Permit
Goto, RecordScreen
F2::
Suspend, Permit
Goto, RecordWindow
RecordScreen:
RecordWindow:
if (Recording or Playing)
return
Coord:=InStr(A_ThisLabel,"Win") ? "Window":"Screen"
LogArr:=[], oldid:="", Log(), Recording:=1, SetHotkey(1)
ShowTip("Recording")
return
F3::
Stop:
Suspend, Permit
if Recording
{
if (LogArr.MaxIndex()>0)
{
s:="`nLoop, 1`n{`n`nSetTitleMatchMode, 2"
. "`nCoordMode, Mouse, " Coord "`n"
For k,v in LogArr
s.="`n" v "`n"
s.="`nSleep, 1000`n`n}`n"
s:=RegExReplace(s,"\R","`n")
FileDelete, %LogFile%
FileAppend, %s%, %LogFile%
s:=""
}
SetHotkey(0), Recording:=0, LogArr:=""
}
else if Playing
{
WinGet, list, List, %Play_Title%
Loop, % list
if WinExist("ahk_id " list%A_Index%)!=A_ScriptHwnd
{
WinGet, pid, PID
WinClose,,, 3
IfWinExist
Process, Close, %pid%
}
SetTimer, CheckPlay, Off
Playing:=0
}
ShowTip()
Suspend, Off
Pause, Off
GuiControl,, Pause, % "[F6] Pause "
isPaused:=0
return
F4::
Play:
Suspend, Permit
if (Recording or Playing)
Gosub, Stop
ahk:=A_IsCompiled ? A_ScriptDir "\AutoHotkey.exe" : A_AhkPath
IfNotExist, %ahk%
{
MsgBox, 4096, Error, Can't Find %ahk% !
Exit
}
Run, %ahk% /r "%LogFile%"
SetTimer, CheckPlay, 500
Gosub, CheckPlay
return
CheckPlay:
Check_OK:=0
WinGet, list, List, %Play_Title%
Loop, % list
if (list%A_Index%!=A_ScriptHwnd)
Check_OK:=1
if Check_OK
Playing:=1, ShowTip("Playing")
else if Playing
{
SetTimer, CheckPlay, Off
Playing:=0, ShowTip()
}
return
F5::
Edit:
Suspend, Permit
Gosub, Stop
Run, notepad.exe "%LogFile%"
return
F6::
Pause:
Suspend, Permit
if Recording
{
Suspend
Pause, % A_IsSuspended ? "On":"Off", 1
isPaused:=A_IsSuspended, Log()
}
else if Playing
{
isPaused:=!isPaused
WinGet, list, List, %Play_Title%
Loop, %list%
if WinExist("ahk_id " list%A_Index%)!=A_ScriptHwnd
PostMessage, 0x111, 65306
}
else return
if isPaused
GuiControl,, Pause, [F6]<Pause>
else
GuiControl,, Pause, % "[F6] Pause "
return
;============ Functions =============
SetHotkey(f:=0) {
; These keys are already used as hotkeys
global UsedKeys
f:=f ? "On":"Off"
Loop, 254
{
k:=GetKeyName(vk:=Format("vk{:X}", A_Index))
if k not in ,Control,Alt,Shift,%UsedKeys%
Hotkey, ~*%vk%, LogKey, %f% UseErrorLevel
}
For i,k in StrSplit("NumpadEnter|Home|End|PgUp"
. "|PgDn|Left|Right|Up|Down|Delete|Insert", "|")
{
sc:=Format("sc{:03X}", GetKeySC(k))
if k not in ,Control,Alt,Shift,%UsedKeys%
Hotkey, ~*%sc%, LogKey, %f% UseErrorLevel
}
SetTimer, LogWindow, %f%
if (f="On")
Gosub, LogWindow
}
LogKey:
LogKey()
return
LogWindow:
LogWindow()
return
LogKey() {
Critical
k:=GetKeyName(vksc:=SubStr(A_ThisHotkey,3))
k:=StrReplace(k,"Control","Ctrl"), r:=SubStr(k,2)
if r in Alt,Ctrl,Shift,Win
LogKey_Control(k)
else if k in LButton,RButton,MButton
LogKey_Mouse(k)
else
{
if (k="NumpadLeft" or k="NumpadRight") and !GetKeyState(k,"P")
return
k:=StrLen(k)>1 ? "{" k "}" : k~="\w" ? k : "{" vksc "}"
Log(k,1)
}
}
LogKey_Control(key) {
global LogArr, Coord
k:=InStr(key,"Win") ? key : SubStr(key,2)
if (k="Ctrl")
{
CoordMode, Mouse, %Coord%
MouseGetPos, X, Y
}
Log("{" k " Down}",1)
Critical, Off
KeyWait, %key%
Critical
Log("{" k " Up}",1)
if (k="Ctrl")
{
i:=LogArr.MaxIndex(), r:=LogArr[i]
if InStr(r,"{Blind}{Ctrl Down}{Ctrl Up}")
LogArr[i]:="MouseMove, " X ", " Y
}
}
LogKey_Mouse(key) {
global gui_id, LogArr, Coord
k:=SubStr(key,1,1)
CoordMode, Mouse, %Coord%
MouseGetPos, X, Y, id
if (id=gui_id)
return
Log("MouseClick, " k ", " X ", " Y ",,, D")
CoordMode, Mouse, Screen
MouseGetPos, X1, Y1
t1:=A_TickCount
Critical, Off
KeyWait, %key%
Critical
t2:=A_TickCount
if (t2-t1<=200)
X2:=X1, Y2:=Y1
else
MouseGetPos, X2, Y2
i:=LogArr.MaxIndex(), r:=LogArr[i]
if InStr(r, ",,, D") and Abs(X2-X1)+Abs(Y2-Y1)<5
LogArr[i]:=SubStr(r,1,-5), Log()
else
Log("MouseClick, " k ", " (X+X2-X1) ", " (Y+Y2-Y1) ",,, U")
}
LogWindow() {
global oldid, LogArr
static oldtitle
id:=WinExist("A")
WinGetTitle, title
WinGetClass, class
if (title="" and class="")
return
if (id=oldid and title=oldtitle)
return
oldid:=id, oldtitle:=title
title:=SubStr(title,1,50)
if (!A_IsUnicode)
{
GuiControl,, MyText, %title%
GuiControlGet, s,, MyText
if (s!=title)
title:=SubStr(title,1,-1)
}
title.=class ? " ahk_class " class : ""
title:=RegExReplace(Trim(title), "[``%;]", "``$0")
s:="tt = " title "`nWinWait, %tt%"
. "`nIfWinNotActive, %tt%,, WinActivate, %tt%"
i:=LogArr.MaxIndex(), r:=LogArr[i]
if InStr(r,"tt = ")=1
LogArr[i]:=s, Log()
else
Log(s)
}
Log(str:="", Keyboard:=0) {
global LogArr
static LastTime
t:=A_TickCount, Delay:=(LastTime ? t-LastTime:0), LastTime:=t
IfEqual, str,, return
i:=LogArr.MaxIndex(), r:=LogArr[i]
if (Keyboard and InStr(r,"Send,") and Delay<1000)
{
LogArr[i]:=r . str
return
}
if (Delay>200)
LogArr.Push("Sleep, " (Delay//2))
LogArr.Push(Keyboard ? "Send, {Blind}" str : str)
}
;============ The End ============
;