Mouse And Keyboard Macro Recorder

Post your working scripts, libraries and tools for AHK v1.1 and older
feiyue
Posts: 348
Joined: 08 Aug 2014, 04:08

Re: Macro Record of Mouse and Keyboard v2.5

17 Jul 2017, 07:05

I updated the script to v2.5, It can be edited in the middle of the recording,
but it also increases the difficulty of beginners. :D

Because my English is not very good, so some sentences I understand are difficult,
my replies are using automatic translation, there must be a lot of inaccurate expression and grammatical errors, I'm sorry.

You master, if you have good ideas, please work together to improve. :bravo:
CAH9t
Posts: 29
Joined: 15 Jul 2017, 11:12
Location: Land

Re: Macro Record of Mouse and Keyboard v2.5

19 Jul 2017, 08:57

You weren't spending time in vain I must say.
1) I absolutely love suspend feature with built-in editor.
2) WinWait(tt, tx="") is a great idea to simplify and shorten the code
3) Mouse coordinates is nice addition
4) If you don't mind my asking, what is your native language, maybe we've got the same? You may send it via private message if you don't want to disclose it.

5) What is it you didn't understand from my previous big post (cause as I see it is a plenty)? Don't be shy to ask. I'll try to be more «automatic translation compatible».

6) Could you look again at least in that peace to give me clue about what I should change in your code to get the desirable effect (I've changed it a bit to clarify) ?
Is it possible to Exclude from the list of scancoded keys all non-language dependable keys (just like you did with LRwin shift ctrl alt they're fine). Those keys are now scancodes in my edition of your script (and that is no good) capslockScrLockNumlck,printscreen,tab,escape,enter, space backspace, insertDel, EndHome pgUpDn , rightLeftUpDown arrows ,appkey, f1-f12, all numpadKeys) ? More so As for the numpad keys they aren't working properly via scancodes at all, acting like numlock is off (you're getting Numinsert NumDel NumEnd NumHome and so on instead of figures - 0 . 1 7 ).
PS. I'm sorry I'm a bit slow with my answers. Sometimes I have a lot on my plate (in that place should've been a joke, but automatic wouldn't digest it :shh: ).
feiyue
Posts: 348
Joined: 08 Aug 2014, 04:08

Re: Macro Record of Mouse and Keyboard v2.6

19 Jul 2017, 13:45

@CAH9t
Hello, thank you very much for your advice on using SC to record keyboard actions. :thumbup:
I haven't tried input from other countries before,
Therefore, I do not know the keyboard layout of different countries
Entering the same key may produce different characters.
Now I updated the script to v2.6, this problem should be fixed.
Also, I'm from China. I'm glad that the website has a Chinese forum. :xmas:
CAH9t
Posts: 29
Joined: 15 Jul 2017, 11:12
Location: Land

Re: Macro Record of Mouse and Keyboard v2.7

21 Jul 2017, 08:44

Hi. You're welcome.
Hell of a job, my friend. Pity I've already figured the scancode-problem for my layout in v2.5, wasn't that hard to bother you with that, and you've already gave me all necessary info for the solution.
1. Nice mouse info section on ctrl.
2. I see you've already fixed«Volume_DownVolume_MuteVolume_Up» problem from v2.5

3. It will be a great addition to have «MouseClick , L\RButton , X, Y, 1, , , R» with R-parameter for clicking context menu items anywhere you want. It's rather popular option in macro recorders by the way.
4. «CoordMode, Mouse, Window» is generally more reliable than «CoordMode, Mouse, Screen». Unless of course you have something special on your mind, I would recommend to set «CoordMode, Mouse, Window» by default and «CoordMode, Mouse, Screen» on checkbox.
5. ControlClick is recording only coordinates not buttons, is it ok?
6. It could be useful to add closing action on "Guiclose:". "WinMinimize, ahk_id %gui_id%" you can achieve via Esc hotkey in #if guiWindow, whick is more comfortable. Also like an option, you may add another button for closing the app.
7. points 4. and 6. are just thoughts. It's easy to adapt them manually.
8. I think the most convenient way of calling the gui is one of your earlier solutions when you're touching with a cursor «x=0 y=0-100» area . «x=0 y=0» is not as responsive by far and it's often used by other scripts.

Best wishes.
feiyue
Posts: 348
Joined: 08 Aug 2014, 04:08

Re: Macro Record for Mouse and Keyboard v2.9

23 Jul 2017, 15:44

@CAH9t, Thank you for your advice. :angel:
I updated the script, some adjustments were made for 4, 6, and 8.
For advice 3, I think that moving relative to the mouse position
is easily affected by user actions, and playback is not accurate enough.

In addition, the ControlClick coordinate mode will automatically
find the control at the coordinates, very convenient,
and click on the location is more accurate.
If it uses the class name of the control, it clicks in the center of the control,
and if the program is updated, the class name may be changed.
CAH9t
Posts: 29
Joined: 15 Jul 2017, 11:12
Location: Land

Re: Macro Record for Mouse and Keyboard v2.9

26 Jul 2017, 07:03

Thank you, for your great and ingenious coding 8-)
#8 really is a nice system for quick stops and resuming.«»
On ctrlclick - OK, I've got your point.

For the 3rd advice, what I really head in mind was the following system (correct me if I'm wrong, pretty fail-safe)
Here's a flow chart:
You hold capslock and system records x1 y1 current mouse coordinates
You make a click anywhere else and system records x2 y2 end coordinates.
You Release capslock and ahk adds another string where «MouseClick , L\RButton , X=x2-x1, Y=y2-y1, 1, , , R»

As you know «MouseClick , L\RButton , X=x2-x1, Y=y2-y1, 1, , , R» =«MouseClick , L\RButton , x2, y2, 1, , , » but it may come in handy if You would need to change manually the original coordinates x1=x1` and y1=y1` for some reason. That is «MouseClick , L\RButton , x2, y2, 1, , ,» but «MouseClick , L\RButton , X=x2-x1, Y=y2-y1, 1, , , R» is still correct .

capslock cause it's being held really rare ( in a way of user activity ) .
It could be a convenient tool for more advanced users, when you need to call context menu from anywhere, for example, As a part of the bigger script to add. Sometimes it's really tedious to calculate relative coordinates. So my point is that would be a useful tool, not means of improving overall reliability.
sancarn
Posts: 224
Joined: 01 Mar 2016, 14:52

Re: Macro Record for Mouse and Keyboard v2.9

26 Jul 2017, 14:55

CAH9t wrote:Thank you, for your great and ingenious coding 8-)
#8 really is a nice system for quick stops and resuming.«»
On ctrlclick - OK, I've got your point.

For the 3rd advice, what I really head in mind was the following system (correct me if I'm wrong, pretty fail-safe)
Here's a flow chart:
You hold capslock and system records x1 y1 current mouse coordinates
You make a click anywhere else and system records x2 y2 end coordinates.
You Release capslock and ahk adds another string where «MouseClick , L\RButton , X=x2-x1, Y=y2-y1, 1, , , R»

As you know «MouseClick , L\RButton , X=x2-x1, Y=y2-y1, 1, , , R» =«MouseClick , L\RButton , x2, y2, 1, , , » but it may come in handy if You would need to change manually the original coordinates x1=x1` and y1=y1` for some reason. That is «MouseClick , L\RButton , x2, y2, 1, , ,» but «MouseClick , L\RButton , X=x2-x1, Y=y2-y1, 1, , , R» is still correct .

capslock cause it's being held really rare ( in a way of user activity ) .
It could be a convenient tool for more advanced users, when you need to call context menu from anywhere, for example, As a part of the bigger script to add. Sometimes it's really tedious to calculate relative coordinates. So my point is that would be a useful tool, not means of improving overall reliability.
Hello. FYI: To write code in a sentence use your code here[/c​]. Your posts are very difficult to read at the moment...

I'm not sure I understand what your 'caps lock' request is personally, and I don't really see the point in it either I'm afraid... Maybe you would be better off with picture search instead? Granted, adding picture search to the macro recorder would be ace! Sometimes you just can't automate applications because they have no positional data. So using picture search could be really really useful for a macro recorder!

It could be a convenient tool for more advanced users, when you need to call context menu from anywhere


Oh do you mean a 'click-relative to the last clicked location'? That is a feature already is it not?
feiyue
Posts: 348
Joined: 08 Aug 2014, 04:08

Re: Macro Record for Mouse and Keyboard v3.0

27 Jul 2017, 00:01

I updated the script to v3.0, the actual delay is no longer recorded,
and the function of the Ctrl key generation code is enhanced.
However, this also increases the difficulty of novices' use. :D
sancarn
Posts: 224
Joined: 01 Mar 2016, 14:52

Re: Macro Record for Mouse and Keyboard v3.0

27 Jul 2017, 12:26

Made some time to test the macro recorder.

I didn't know you already added image searching. But how exactly do you use the image search?

Press Control > Hover over image search button > Press Control > "now what???"


**EDIT:**

Oh you don't... (Looking at the code now). Although this might be fixable with GDI+ :D
CAH9t
Posts: 29
Joined: 15 Jul 2017, 11:12
Location: Land

Re: Macro Record for Mouse and Keyboard v3.0

29 Jul 2017, 06:57

sancarn, As I see you've already got the point about relative clicks ( I really tried to make it obvious as hell ) ? it's not at all a rare feature in macro recorders.
I gave a flow chart, not the real code , in my book there would be a confusion if I'd brace it into code brackets. Do you have blocks for the due flow chart, here? And I don't see any use in coding single simple commands like

Code: Select all

click, x, y, Relative
to make them look cool, so I won't :angel: .

ps. By the way, you could use to read the stuff more attentively.
sancarn
Posts: 224
Joined: 01 Mar 2016, 14:52

Re: Mouse And Keyboard Macro Recorder v3.0

01 Aug 2017, 12:18

@feiyue

You might want to implement this feature into your macro recorder. My vision is the following:
  • 1. You click on 'Insert an image search command'
    2. The user selects a portion of the screen representing the picture.
    3. The picture is saved and the filepath is included in the image search command in the macro recorder section.
    ------
    4. When the 'project' is saved, the pictures are also moved so they are relative to the ahk script that was saved. And the paths within the script would also require altering.
Anyway, I have written some code implementing the screenshot system. Try pressing Win+S while running this script. P.S. When you draw the screenshot rectangle, a red rectangle should also appear around the section you are screenshotting.

By looking at the code you can see that I, to take the screenshot, only have to do:

Code: Select all

GoSub, TAKE_A_SCREENSHOT
I will leave the code generation and such up to you though :)
RoyBlumenthal

Re: Mouse And Keyboard Macro Recorder v3.0

02 Aug 2017, 09:12

Hi Guys...

I'm really sorry that I'm being so ignorant! I simply haven't got a clue how to use this script!

I've saved the file as an AHK extension. I double click on it. And it seems to run the script, cos I see a little "H" logo in my "show hidden icons" window in the taskbar. But nothing seems to be happening.

This is what I'm actually trying to achieve...

1. I use a keyboard shortcut in PHOTOSHOP: SHIFT-CONTROL-ALT-N. This creates a new layer above the one I'm working on.

2. On my previous machine, a Wacom Cintiq Companion 2, I was able to assign that keyboard shortcut string to a hotkey on the device. My machine is in for repair. And Wacom has loaned me a newer machine, the Wacom Studio Pro. In this machine, it simply will not accept the SHIFT part of the keyboard shortcut. All I want is to be able to assign that string to the hotkey.

Is this script the right thing for me to be attempting to use? Is there a better way of doing it?

3. I have a second keyboard shortcut that also needs assigning to one of the hot buttons on the device... SHIFT-CONTROL-ALT-N CONTROL-] CONTROL-]. This basically makes a new layer above the one I'm on, then moves it down twice in the layer stack. Effectively making a new layer BELOW the one I'm on.

Thanks for any help that may come from my ignorant plea!

Blue skies
Roy
sancarn
Posts: 224
Joined: 01 Mar 2016, 14:52

Re: Mouse And Keyboard Macro Recorder v3.0

02 Aug 2017, 14:34

RoyBlumenthal wrote:Hi Guys...

I'm really sorry that I'm being so ignorant! I simply haven't got a clue how to use this script!

I've saved the file as an AHK extension. I double click on it. And it seems to run the script, cos I see a little "H" logo in my "show hidden icons" window in the taskbar. But nothing seems to be happening.

This is what I'm actually trying to achieve...

1. I use a keyboard shortcut in PHOTOSHOP: SHIFT-CONTROL-ALT-N. This creates a new layer above the one I'm working on.

2. On my previous machine, a Wacom Cintiq Companion 2, I was able to assign that keyboard shortcut string to a hotkey on the device. My machine is in for repair. And Wacom has loaned me a newer machine, the Wacom Studio Pro. In this machine, it simply will not accept the SHIFT part of the keyboard shortcut. All I want is to be able to assign that string to the hotkey.

Is this script the right thing for me to be attempting to use? Is there a better way of doing it?

3. I have a second keyboard shortcut that also needs assigning to one of the hot buttons on the device... SHIFT-CONTROL-ALT-N CONTROL-] CONTROL-]. This basically makes a new layer above the one I'm on, then moves it down twice in the layer stack. Effectively making a new layer BELOW the one I'm on.

Thanks for any help that may come from my ignorant plea!

Blue skies
Roy
Hi Roy,

Yeah I too was a bit confused when starting up the application because I didn't see anything happenning.

You need to move your mouse to the far left hand side of the screen to see the GUI (Graphical User Interface)
Is this script the right thing for me to be attempting to use? Is there a better way of doing it?
No. This script will not help you here really. Remapping keys is something you should just know

As for #3, try:

Code: Select all

Create_New_Layer:
    Send,^+!N
    Send,^]
    Send,^]
return
feiyue
Posts: 348
Joined: 08 Aug 2014, 04:08

Re: Mouse And Keyboard Macro Recorder v3.0

02 Aug 2017, 23:59

This tool is just for beginners to automatically generate code, for the master, it may not have much effect. :D
Another tool I release, FindText(), is a very good tool for writing automated scripts,
which implements the search for images and text, and does not generate additional picture files.
It's good for a master.
sancarn
Posts: 224
Joined: 01 Mar 2016, 14:52

Re: Mouse And Keyboard Macro Recorder v3.0

03 Aug 2017, 01:33

feiyue wrote:This tool is just for beginners to automatically generate code, for the master, it may not have much effect. :D.
What? And the beginner will easily be able to program the image search? I think it's much easier for a noob to figure out how prewritten code is working than have to write code themselves, but whatever floats your boat. It's alright if you have a different opinion, but the way I see it is the more you add, the easier it is for a noob to write their own complicated scripts. That's just my opinion though.
feiyue
Posts: 348
Joined: 08 Aug 2014, 04:08

Re: Mouse And Keyboard Macro Recorder v3.5

03 Aug 2017, 17:35

I updated the script to v3.5, added a lot of code. :D
Now it can automatically generate ImageSearch code and Coordinate relative image origin code.
Additionally, the Debug button is added to the GUI interface to debug stuck rows.
User avatar
Relayer
Posts: 160
Joined: 30 Sep 2013, 13:09
Location: Delaware, USA

Re: Mouse And Keyboard Macro Recorder v3.5

04 Aug 2017, 09:37

A couple of suggestions:

I find that I want to save macros for future use. I name my macros with the extension ".ahm". It would be nice if your program had an open/save/saveas feature for creating/using/editing macros. The user could name therm whatever they wanted. The user can associate the .ahm with the AutoHotkey exe so the macro can run on double click.

Another nice feature is to add an edit box so the user can input the number of loops to execute instead of needing to interrupt an infinite loop.

You may want to consider forking your design to keep a much simpler macro-maker for those that do not need the complexity that goes along with dealing with images. An image-capable version could be the second version. Simplicity, for most people, will minimize the number of 'second order' problems that might cause people to stop using the tool.

Here is your V3.0 with my embellishments. BTW, I tend to want to convert your Goto statements to Gosub and indent with tabs instead of spaces. Just preference.

Code: Select all

/*
-----------------------------------
  Macro Record v3.0  By FeiYue

  Description:

  1. This script records the mouse and keyboard
     actions and then plays back.

  2. Use the mouse to shake on the left edge of the screen
     to show or hide the GUI window, It reduces the use of hotkeys.
     you can change and insert code during the recording process,
     When the GUI window is displayed, the recording is automatically paused,
     and even minimize GUI window, it is paused.
     When the Gui window is hidden,the recording will continue automatically.

  3. If you want to stop playback process,
     please press the Pause hotkey first,
     and then click the Stop button in the GUI.

  4. You can press the Ctrl key individually to display a menu,
     Press the Ctrl key again to select the items in the menu.
     You can switch the relative coordinate mode, add search
     the picture code, mouse movement code, delay code,
     color matching code, and cursor matching code.

  5. If you want to record the mouse action in ControlClick mode,
     You can let the CtrlClick CheckBox be selected.
     Menu, etc. window non client area cannot use ControlClick,
     Even in the window client area, some places are not good.
     Sometimes you need to change the number of Clicks to 2,
     for the first time target control gets the focus, but
     watch for any side effects. (Beginners are not recommended)

  6. If you need to find and batch replacement,
     you can click the Edit button, Edit by Notepad, closed Notepad,
     the new content will automatically update to the GUI edit box.

  Pause  Button  -->  Pause  Record/Play (Better to use hotkey)
  Record Button  -->  Record Mouse/Keyboard/Window
  Stop   Button  -->  Stop   Record/Play (Save To LogFile)
  Play   Button  -->  Play   LogFile
  Edit   Button  -->  Edit   LogFile

  Coord Win  CheckBox --> Coordinate relative to active window
  Add Titile CheckBox --> Additional title when recording window
  CtrlClick  CheckBox --> Additional ControlClick code
  Loop Play  CheckBox --> Loop playback

  Pause  Hotkey  -->  Pause  Record/Play
-----------------------------------
*/

#NoEnv
#SingleInstance force
SetBatchLines, -1
CoordMode, Mouse
CoordMode, Pixel
CoordMode, ToolTip
;LogFile = %A_Temp%\~Record.txt
LogFile = %A_ScriptDir%\~Record.ahm
;----------------------------
Gui, +AlwaysOnTop +Resize +Hwndgui_id
Gui, Color, DDEEFF
Gui, Margin, 15, 15
Gui, Font, s14, Verdana
s=Open,SaveAs,Pause,Record,Stop,Play,Edit,Clear,Exit
For i,v in StrSplit(s, ",")
	Gui, Add, Button, w125 h40 gRun, %v%
Gui, Add, CheckBox, wp h30 vCoordWin Checked, Coord Win
Gui, Add, CheckBox, wp hp vAddTitle, Add Title
Gui, Add, CheckBox, wp hp vCtrlClick, CtrlClick
Gui, Add, CheckBox, wp hp vLoopPlay, Loop Play
Gui, Add, Edit, yp+40 wp h30 vLoopCount, 1
Gui, Add, Text, yp+30 wp hp, % "         times"
Gui, Add, Edit, % "ym w650 h" . (40*7+30*4+15*10)
	. " -Wrap HScroll vMyEdit HwndMyEditHwnd"
Gui, Show, Hide, Macro Record v3.0
OnExit, SaveEditExit
Gosub, ReadEdit
SwitchGUI()
;----------------------------
	Hotkey, *Pause, Pause
;----------------------------
return

SwitchGUI() {
	Gui, Switch:+AlwaysOnTop -Caption +ToolWindow
		+E0x08000000  ; WS_EX_NOACTIVATE = 0x08000000
	Gui, Switch:Color, White
	Gui, Switch:Show, NA x0 y0 w1 h%A_ScreenHeight%
	OnMessage(0x200, "WM_MOUSEMOVE")
	SetTimer, SwitchOnTop, 2000
	return
	SwitchOnTop:
	ListLines, Off
	Gui, Switch:+AlwaysOnTop
	return
}

WM_MOUSEMOVE() {
	ListLines, Off
	static Time, OkTime
	if (A_Gui="Switch") and (Time:=A_TickCount)>OkTime
	{
		OkTime:=Time+500
		Pause, Off
		SetTimer, GuiShow, -10
	}
}

GuiSize:
if ErrorLevel=1
	return
w:=A_GuiWidth-170, h:=A_GuiHeight-30
GuiControl, Move, MyEdit, w%w% h%h%
return

ReadEdit:
FileRead, s, %LogFile%
GuiControl,, MyEdit, %s%
s=
return

SaveEdit:
GuiControlGet, s,, MyEdit
FileDelete, %LogFile%
FileAppend, %s%, %LogFile%
s=
return

SaveEditExit:
Gosub, SaveEdit
ExitApp

Clear:
GuiControlGet, s,, MyEdit
s:=s="" ? olds : (olds:=s)/0
GuiControl,, MyEdit, %s%
GuiControl, Focus, MyEdit
return

Open:
	Gui, -AlwaysOnTop
	tmp := Dlg_FileSelect("", "*.ahm")
	if (FileExist(tmp))
	{
		LogFile := tmp
		Gosub ReadEdit
	}
	Gui, +AlwaysOnTop
Return

SaveAs:
	Gui, -AlwaysOnTop
	;FileSelectFile, tmp, S8, %LogFile%,, *.ahm
	tmp := Dlg_FileSelect(LogFile, "*.ahm",, "S8")
	if (tmp)
	{
		LogFile := tmp . (InStr(tmp, ".ahm") ? "" : ".ahm")
		Gosub SaveEdit
	}
	Gui, +AlwaysOnTop
Return

Exit:
ExitApp

Run:
if IsLabel(s:=A_GuiControl)
	;Goto, %s%
	Gosub %s%
return

;========== Hotkey Label ==========


GuiShow:
Suspend, Permit
IfWinExist, ahk_id %gui_id%
{
	;Goto, GuiClose
	Gosub GuiClose
	Return
}
Suspend, On
Gui, Show
GuiControl, Focus, MyEdit
if !Recording
	return
Logs:=Trim(Logs, "`n")
IfEqual, Logs,, return
GuiControlGet, s,, MyEdit

;   ____________________________
;__/____  set boiler plate  ____\____
/*
	This creates a code block as text in the variable 'r'.
	The construct to concatenate the text uses a Join continuation
	with the '%' option, i.e. considers the percent symbol as a literal
	character in the text to be joined and NOT a variable reference.
		
*/
r := ""
if !InStr(s, "WinWait(title, text=")
	r=%r%
(%

WinWait(title, text="")
{
	CoordMode, ToolTip
	ToolTip, Waiting... %title%, 80, 0
	WinWaitActive, %title%, %text%, 3
	if ErrorLevel
	{
		WinWait, %title%, %text%
		WinActivate
		Sleep, 500
	}
	ToolTip
}

)
if !InStr(s, "Click(coordwin, x, y, other=")
	r=%r%
(%

Click(coordwin, x, y, other="")
{
	if (r:=coordwin ? "Window":"Screen")!=A_CoordModeMouse
		CoordMode, Mouse, %r%
	Click, %x%, %y%, %other%
	Sleep, InStr(other,"R") ? 500 : 100
}

)
if !InStr(s, "Send(key=")
	r=%r%
(%

Send(key="")
{
	IfEqual, key,, return
	Send, {Blind}%key%
	Sleep, 200
}

)
if !InStr(s, "GetColor(coordwin, x, y, other=")
	r=%r%
(%

GetColor(coordwin, x, y, other="RGB")
{
	if (r:=coordwin ? "Window":"Screen")!=A_CoordModePixel
		CoordMode, Pixel, %r%
	PixelGetColor, color, x, y, %other%
	return, color
}

)
;__ _______________________________ ____
;  \____  end of boiler plate  ____/

Logs:=StrReplace(Logs, "+-", "-")
Logs:=StrReplace(r "`n" Logs "`n", "`n", "`r`n")
ControlSend,, {Home}, ahk_id %MyEditHwnd%
Control, EditPaste, %Logs%,, ahk_id %MyEditHwnd%
ControlSend,, {Left}{Right}, ahk_id %MyEditHwnd%
Logs:=s:=r:=""
return

GuiClose:
WinMinimize, ahk_id %gui_id%
Gui, Hide
Suspend, Off
GuiControlGet, AddTitle
GuiControlGet, CtrlClick
return


Record:
Suspend, Permit
GuiControl, Focus, MyEdit
if (Recording or Playing)
	return
Gosub, GuiClose
GuiControl, Disable, Record
Recording:=1, IsPaused:=0, SetHotkey(1)
Logs:=oldtt:=""
GuiControlGet, CoordWin
ToolTip, % "  Recording  ", 0, 0
return


Stop:
Suspend, Permit
GuiControl, Focus, MyEdit
if Recording
{
	IfWinNotExist, ahk_id %gui_id%
		Gosub, GuiShow
	GuiControl, Enable, Record
	SetHotkey(0), Recording:=0, IsPaused:=0
	ToolTip
	return
}
LoopPlay:=0
SetTitleMatchMode, 2
DetectHiddenWindows, On
SplitPath, LogFile, FileName
WinGet, list, List, %FileName% ahk_class AutoHotkey
Loop, %list%
{
	id:=list%A_Index%
	if (id=A_ScriptHwnd) or !WinExist("ahk_id " id)
		Continue
	WinGet, pid, PID
	WinClose
	WinWaitClose,,, 3
	if ErrorLevel
		Process, Close, %pid%
}
return


Play:
Suspend, Permit
GuiControl, Focus, MyEdit
if Playing
	return
if Recording
	Gosub, Stop
Gosub, SaveEdit
Gosub, GuiClose
Playing:=1
ToolTip, % "  Playing  ", 0, 0
ListLines, Off
GuiControlGet, LoopPlay
GuiControlGet, LoopCount
Loop % (tmp := LoopPlay ? LoopCount : 1)
{
	RunWait, %A_AhkPath% "%LogFile%"
	if !LoopPlay
		Break
}
ListLines, On
ToolTip, % "  DONE... Macro was run " . tmp . " times."
Sleep, 2500 * 2
Tooltip
Playing:=0
return


Edit:
Suspend, Permit
GuiControl, Focus, MyEdit
Gui, -AlwaysOnTop
Gosub, SaveEdit
FileGetTime, time1, %LogFile%
RunWait, notepad++.exe "%LogFile%"
FileGetTime, time2, %LogFile%
if (time1!=time2)
	Gosub, ReadEdit
Gui, +AlwaysOnTop
return


Pause:
Suspend, Permit
GuiControl, Focus, MyEdit
if Recording
{
	IsPaused:=!IsPaused, SetHotkey(!IsPaused)
	ToolTip, % IsPaused ? "  Record Pause  "
		: "  Recording  ", 0, 0
  return
}
SetTitleMatchMode, 2
DetectHiddenWindows, On
SplitPath, LogFile, FileName
WinGet, list, List, %FileName% ahk_class AutoHotkey
Loop, %list%
{
	id:=list%A_Index%
	if (id=A_ScriptHwnd) or !WinExist("ahk_id " id)
		Continue
	PostMessage, 0x111, 65306
}
return


;========== Function and Label ==========


SetHotkey(f=0)
{
	; These keys are already used as hotkeys
	static ExcludeKeys:="Pause"
	ListLines, Off
	f:=f ? "On":"Off"
	For i,k in StrSplit("NumpadEnter|Home|End|PgUp"
		. "|PgDn|Left|Right|Up|Down|Delete|Insert", "|")
	{
		k:=Format("sc{:X}", GetKeySC(k))
		Hotkey, ~*%k%, LogKey, %f% UseErrorLevel
	}
	Loop, 254
	{
		k:=GetKeyName(vk:=Format("vk{:X}", A_Index))
		if k not in ,Control,Alt,Shift,%ExcludeKeys%
			Hotkey, ~*%vk%, LogKey, %f% UseErrorLevel
	}
	SetTimer, CheckWindow, % f="On" ? 100 : f
	ListLines, On
}

LogKey:
Critical
k:=GetKeyName(vk:=SubStr(A_ThisHotkey,3))
k:=StrReplace(k,"Control","Ctrl"), r:=SubStr(k,2)
if r in Win,Alt,Ctrl,Shift,Button
	if IsLabel(k)
	{
		;Goto, %k%
		Gosub %k%
		Return
	}
; Some input auto completion and send the left or right
; key for the cursor center, excluding these key records
if (k="NumpadLeft" or k="NumpadRight")
	and !GetKeyState(k,"P")
		return
k:=StrLen(k)>1 ? "{" k "}" : k~="\w" ? k : "{" vk "}"
Log(k,1)
return

LCtrl:  ; Individually press Ctrl to get multiple information
RCtrl:
if CoordWin=1
	CoordMode, Mouse, Window
MouseGetPos, Ctrl_X, Ctrl_Y
CoordMode, Mouse, Screen
MouseGetPos, Ctrl_ScreenX, Ctrl_ScreenY
LWin:
RWin:
LAlt:
RAlt:
LShift:
RShift:
Log("{" . (InStr(k,"Win") ? k:r) . " Down}",1)
Critical, Off
KeyWait, %A_ThisLabel%
Critical
k:=A_ThisLabel, r:=SubStr(k,2), LastTime:=A_TickCount
Log("{" . (InStr(k,"Win") ? k:r) . " Up}",1)
;----------------------------
if InStr(k,"Ctrl")
	and SubStr(Logs,1-22)="{Ctrl Down}{Ctrl Up}"")"
{
	r:=SubStr(Logs, InStr(Logs,"`n",0,0)+1)
	Logs:=r="Send(""{Ctrl Down}{Ctrl Up}"")"
		? SubStr(Logs,1,-StrLen(r))
		: SubStr(Logs,1,-22) . """)"
	;Goto, CtrlMenu
	Gosub CtrlMenu
}
return

CtrlMenu:
PixelGetColor, color, Ctrl_ScreenX, Ctrl_ScreenY, RGB
Suspend, On
Gui, CtrlMenu:+LastFoundExist
IfWinNotExist
{
	Gui, CtrlMenu:New
	Gui, +AlwaysOnTop -Caption +ToolWindow
		+LastFound +Hwndctrl_id +E0x08000000
	Gui, Margin, 0, 0
	Gui, Color, DDEEFF
	Gui, Font, s14, Verdana
	s=Relative Screen,Relative Window
		,Relative Origin,Add ImageSearch
		,Add MouseMove,Add Sleep 1000
		,Add if Color,Add if Cursor
	For i,v in StrSplit(s, ",", " ")
		Gui, Add, Button, w250 h40, %v%
	Gui, Show, Hide, CtrlMenu
}
WinGetPos,,, W, H
MouseGetPos, X, Y
X:=X>A_ScreenWidth-W-5 ? X-W-2 : X+2
Y:=Y>A_ScreenHeight-H-5 ? Y-H-2 : Y+2
Gui, CtrlMenu:Show, NA x%X% y%Y%
KeyWait, Ctrl, D
MouseGetPos,,, id, class
Gui, CtrlMenu:Hide
KeyWait, Ctrl
Suspend, Off
;----------------------------
if (id!=ctrl_id) or !InStr(class,"Button")
	return
X:=CoordWin=2 ? "X+" (Ctrl_ScreenX-originX) : Ctrl_X
Y:=CoordWin=2 ? "Y+" (Ctrl_ScreenY-originY) : Ctrl_Y
i:=SubStr(class,7)
if i=1
	CoordWin=0
else if i=2
	CoordWin=1
else if i=3
{
	CoordWin:=2
	originX:=Ctrl_ScreenX, originY:=Ctrl_ScreenY
	Log(";-- Origin`nX:=" originX ", Y:=" originY)
}
else if i=4
	Log(";-- Origin`nCoordMode, Pixel`nImageSearch, X, Y
		, 0, 0, A_ScreenWidth, A_ScreenHeight, Pic")
else if i=5
	Log(";-- `nClick(" (CoordWin=1) ", " X ", " Y ", 0)")
else if i=6
	Log("Sleep, 1000")
else if i=7
	Log("if (GetColor(" (CoordWin=1) ", " X
		. ", " Y ")=""" color """)`n{`n`n}")
else if i=8
	Log("if (A_Cursor=""" A_Cursor """)`n{`n`n}")
return

LButton:
RButton:
MButton:
if CoordWin=1
	CoordMode, Mouse, Window
MouseGetPos, %k%_X, %k%_Y
CoordMode, Mouse, Screen
MouseGetPos, X, Y, id
%k%_ScreenX:=X, %k%_ScreenY:=Y
if (CtrlClick and WinExist("A")=id)
{
	WinGetPos, WinX, WinY
	i:=X-WinX, j:=Y-WinY, r:=SubStr(k,1,1)
	s=; ControlClick, x%i% y%j%, `% "%oldtt%"
		,, %r%, 1, NA`n
} else s=
Log(";-- `n" s "Click(" (CoordWin=1) ", "
	. (CoordWin=2 ? "X+" (X-originX) : %k%_X) ", "
	. (CoordWin=2 ? "Y+" (Y-originY) : %k%_Y)
	. ", """ . SubStr(k,1,1) . " D"")")
%k%_T:=A_TickCount
SetTimer, CheckWindow, Off
Critical, Off
KeyWait, %A_ThisLabel%
Critical
SetTimer, CheckWindow, On
k:=A_ThisLabel
MouseGetPos, X, Y
if (A_TickCount-%k%_T)>200
	i:=X-%k%_ScreenX, j:=Y-%k%_ScreenY
else i:=j:=0
r:=SubStr(Logs, InStr(Logs,"`n",0,0)+1)
if InStr(r,"Click")=1 and InStr(r," D"")")
	and Abs(i)+Abs(j)<5
		Logs:=SubStr(Logs,1,-4) . """)"
else
	Log("Click(" (CoordWin=1) ", "
		. (CoordWin=2 ? "X+" (X-originX) : %k%_X+i) ", "
		. (CoordWin=2 ? "Y+" (Y-originY) : %k%_Y+j)
		. ", """ . SubStr(k,1,1) . " U"")")
return

CheckWindow:
ListLines, Off
if A_IsSuspended
	return
Critical
WinGetTitle, tt, A
WinGetClass, tc, A
if (tt="" and tc="") or (tc="" and !AddTitle)
	return
tt:=(AddTitle ? SubStr(tt,1,50):"")
	. (tc ? " ahk_class " tc:"")
tt:=RegExReplace(Trim(tt), "[;``]", "``$0")
tt:=StrReplace(tt,"""","""""")
if (tt=oldtt)
	return
oldtt:=tt
r:=SubStr(Logs, InStr(Logs,"`n",0,0)+1)
if InStr(r,"WinWait")=1
	Logs:=SubStr(Logs,1,-StrLen(r))
Log("WinWait(""" . tt . """)")
return

Log(str, Keyboard=0)
{
	global Logs, LastTime
	Delay:=(Time:=A_TickCount)-LastTime, LastTime:=Time
	r:=SubStr(Logs, InStr(Logs,"`n",0,0)+1)
	if (Keyboard and InStr(r,"Send")=1 and Delay<1000)
	{
		Logs:=SubStr(Logs,1,-2) . str """)"
		return
	}
	Logs:=RTrim(Logs,"`n") . "`n`n"
		. (Keyboard ? "Send(""" str """)" : str)
}


;============ The End =============

;
Relayer
User avatar
SpeedMaster
Posts: 494
Joined: 12 Nov 2016, 16:09

Re: Mouse And Keyboard Macro Recorder v3.5

04 Aug 2017, 14:23

Hello,
:thumbup: :wave:
Last edited by SpeedMaster on 28 Apr 2018, 15:29, edited 1 time in total.
sancarn
Posts: 224
Joined: 01 Mar 2016, 14:52

Re: Mouse And Keyboard Macro Recorder v3.5

06 Aug 2017, 12:40

Relayer wrote:I name my macros with the extension ".ahm".
Why would you name the macros with their own special extension? To me this feels a bit pointless (unless you are storing extra data)... Especially when an AHK file can be saved and opened in the same manner as any other file (with this program). To me, this will just confuse people.
Relayer wrote:Another nice feature is to add an edit box so the user can input the number of loops to execute instead of needing to interrupt an infinite loop.
Perhaps a conditional break.
User avatar
SpeedMaster
Posts: 494
Joined: 12 Nov 2016, 16:09

Re: Mouse And Keyboard Macro Recorder v3.5

07 Aug 2017, 18:04

Thanks :clap:
Last edited by SpeedMaster on 09 Nov 2021, 05:56, edited 2 times in total.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 85 guests