Mouse And Keyboard Macro Recorder

Post your working scripts, libraries and tools for AHK v1.1 and older
Krd
Posts: 405
Joined: 10 Mar 2020, 02:46

Re: Mouse And Keyboard Macro Recorder

26 Jun 2020, 03:03

Anyone know how to change the mouse to Clicks instead of Mouseclick?

https://www.autohotkey.com/docs/commands/Click.htm
myahk55
Posts: 3
Joined: 02 Jul 2020, 10:03

Re: Mouse And Keyboard Macro Recorder

02 Jul 2020, 11:16

Hello,

In order to create this script Do I copy and past the entire script and then save as a .pmc file.

How to load and run it in PMC?

I am totally new to PMC but I am ok with AHK.

Thanks!
Erkko_68
Posts: 1
Joined: 05 Jul 2020, 05:01

Re: Mouse And Keyboard Macro Recorder

05 Jul 2020, 05:12

Why my mouse move too fast when I play back the recorded movement?
RiseUp
Posts: 28
Joined: 01 Oct 2013, 21:27

Re: Mouse And Keyboard Macro Recorder

13 Jul 2020, 17:02

myahk55 wrote:
02 Jul 2020, 11:16
Hello,

In order to create this script Do I copy and past the entire script and then save as a .pmc file.

How to load and run it in PMC?

I am totally new to PMC but I am ok with AHK.

Thanks!
You should save it as an .ahk file, as it is an AutoHotkey script. It has nothing to do with PMC.
thejack473
Posts: 6
Joined: 21 Feb 2017, 07:30

Re: Mouse And Keyboard Macro Recorder

06 Oct 2020, 04:35

sorry but i'm pretty new to this
Macro Recorder v2.1 By FeiYue is beautifully simple. but i can't figure out how to put the loop in an actual macro script. i would love if there was an extra "save" button that where i could choose the key to activate the macro and be done with it.
but if anyone would tell me how to bind the loop to a key i would be very greatful.

i tried

Code: Select all

F10::
loop, 1
{
;code goes here
}
return
but all it did was highlight the windows shortcuts and do the first click. like i had used : rather than :: or something, i thought ahk would handle it differently
tuanquang162
Posts: 1
Joined: 14 Nov 2020, 01:22

Re: Mouse And Keyboard Macro Recorder

14 Nov 2020, 01:27

How can i loop this script countless times? Thank you!
madensuyu1
Posts: 4
Joined: 13 Oct 2021, 01:16

Re: Mouse And Keyboard Macro Recorder

13 Oct 2021, 15:02

this is great but it repeats the process very fast so it is necessary to add a "sleep" after every recording.
RandomSquirrel
Posts: 1
Joined: 08 Nov 2021, 05:33

Re: Mouse And Keyboard Macro Recorder

08 Nov 2021, 05:39

Which line can I adjust the playback speed? I find the recorder plays it back about 3x the speed in which I recorded.
User avatar
SpeedMaster
Posts: 494
Joined: 12 Nov 2016, 16:09

Re: Mouse And Keyboard Macro Recorder

09 Nov 2021, 06:04

tuanquang162 wrote:
14 Nov 2020, 01:27
How can i loop this script countless times? Thank you!
RandomSquirrel wrote:
08 Nov 2021, 05:39
Which line can I adjust the playback speed?
feiyue wrote:
11 Mar 2020, 23:24
Updated to 2.1 version - 2020/03/12
1. Put the record file in the temporary folder to avoid that some computers cannot generate the record file.
2. Record the actual delay instead of a uniform delay of 0.5 seconds.
... Note: if the actual delay is large, please modify the record file yourself.
3. I added "Loop, 1 {...}" statement at the beginning and end of the record file,
... You can modify the number of loops by yourself. For infinite loops, you can use "Loop {".
ukanuk
Posts: 2
Joined: 27 Nov 2021, 20:05
Contact:

Re: Mouse And Keyboard Macro Recorder

27 Nov 2021, 20:49

Thank you so much @feiyue! I created a forum account just to comment here!

I tried Pulover's Macro Creator but it wasn't working for me. All I want to do is occasional on-the-fly macro recording, with a single key for recording and playback and no GUI interaction required (a GUI is great for learning, but I do not want to have to move my mouse to click on anything). I think PMC should do that with the "Hotkeys Always Active" and "Clear List" options enabled, but for some reason it would never erase the existing script unless I manually deleted it with the GUI, and after several hours of effort and YouTube videos I finally gave up. I am a new user and really appreciate you trying to keep it simple.

In any case, I have some feedback for your script if you're interested:
  • It would help me if the screen tip that currently says "Recording" and "Playing" could additionally say "Recording (Paused)" and "Playing (Paused)".
  • Could you add a note in the script on how to turn off the GUI? Once I've memorized the hotkeys I'd like to disable it, but I'm not sure how to make that change.
  • Could your script rename the Pause label to something like LabelPause? I tried replacing F6 on line 196 with Pause so I could use the Pause key on my keyboard (I need F6 for its existing function). But when starting the script I get this error:
    Error at line 197.

    Line text: Pause
    Error: Duplicate label.

    The program will now exit.
    I think it's a an issue with the you having a label named Pause, which is the same name Autohotkey uses for Pause from the keyboard.
User avatar
SpeedMaster
Posts: 494
Joined: 12 Nov 2016, 16:09

Re: Mouse And Keyboard Macro Recorder

28 Nov 2021, 14:48

I like the script the way it is now. Simple and easy to use. 8-)
Cheers
Last edited by SpeedMaster on 19 Mar 2023, 12:39, edited 1 time in total.
User avatar
SpeedMaster
Posts: 494
Joined: 12 Nov 2016, 16:09

Re: Mouse And Keyboard Macro Recorder

28 Nov 2021, 15:32

ukanuk wrote:
27 Nov 2021, 20:49
Could you add a note in the script on how to turn off the GUI? Once I've memorized the hotkeys I'd like to disable it, but I'm not sure how to make that change.
Here is the way I use when I run the script from my main script.
:!: Be careful to put these shortcuts in your main script and not in the macro recorder script and adapt the file path as needed. "C:\MouseAndKeyboardMacroRecorder.ahk"

Shortcuts:
Win + F1 ................... Start visible
Win + F2 ................... Start hidden

Code: Select all

; start visible
#f1::
run, C:\MouseAndKeyboardMacroRecorder.ahk
sleep, 500
WinShow, Macro Recorder
return

;start hidden
#f2::
run, C:\MouseAndKeyboardMacroRecorder.ahk
sleep, 800
WinHide, Macro Recorder
return
.
ukanuk wrote:
27 Nov 2021, 20:49
I think it's a an issue with the you having a label named Pause, which is the same name Autohotkey uses for Pause from the keyboard.
just do that...

Code: Select all

Pause::
;pause:
and replace the corresponding lines with these

Code: Select all

UsedKeys:="F1,F2,F3,F4,F5,Pause"
s:="[F1]Record(Screen),[F2]Record(Window),"
  . "[F3]Stop,[F4]Play,[F5]Edit,[Pause]Pause "
Cheers
Krd
Posts: 405
Joined: 10 Mar 2020, 02:46

Re: Mouse And Keyboard Macro Recorder

21 Dec 2021, 07:07

Any one knows how to remove app and window detections? I just want eveything else like Click, keyboard, Sleep. In apps I use this it is a mess to clean it by removing all detected windows which are diffrent next time I run my scripts. So it is useless for me due to restrictions in my system.

I appreciate any input.
User avatar
SpeedMaster
Posts: 494
Joined: 12 Nov 2016, 16:09

Re: Mouse And Keyboard Macro Recorder

21 Dec 2021, 16:03

Krd wrote:
21 Dec 2021, 07:07
Any one knows how to remove app and window detections? I just want eveything else like Click, keyboard, Sleep....
Try v2.1+ :arrow: viewtopic.php?f=6&t=34184&p=158990#p158990

Cheers,
Last edited by SpeedMaster on 19 Mar 2023, 12:42, edited 1 time in total.
Krd
Posts: 405
Joined: 10 Mar 2020, 02:46

Re: Mouse And Keyboard Macro Recorder

22 Dec 2021, 02:14

@SpeedMaster

Thanks.
When I do recording and run this code after, I get warning:
This variable has not been assigned a value.
Specifically: LoFie (Aglobal variable)
....> Loop, read, % LogFile, % LogFile2

I am sure im doing something wrong.
User avatar
SpeedMaster
Posts: 494
Joined: 12 Nov 2016, 16:09

Re: Mouse And Keyboard Macro Recorder

22 Dec 2021, 05:19

I hope this will work.
Last edited by SpeedMaster on 19 Mar 2023, 12:45, edited 1 time in total.
Krd
Posts: 405
Joined: 10 Mar 2020, 02:46

Re: Mouse And Keyboard Macro Recorder

22 Dec 2021, 05:37

@SpeedMaster


Didn't work.

Might be because I use the Windows Store edition of ahk? Can't acces it s folder tho.

No other direct way like removing some of the original code in this script?
User avatar
SpeedMaster
Posts: 494
Joined: 12 Nov 2016, 16:09

Re: Mouse And Keyboard Macro Recorder

22 Dec 2021, 06:16

Krd wrote:
22 Dec 2021, 05:37
Didn't work.
No other direct way like removing some of the original code in this script?
you can try to disable the call to the LogWindow() function in the script

disable the corresponding line like that:

Code: Select all

LogWindow:
;LogWindow()
return
but I don't know if there won't be any bugs. :?
Krd
Posts: 405
Joined: 10 Mar 2020, 02:46

Re: Mouse And Keyboard Macro Recorder

22 Dec 2021, 08:21

@SpeedMaster

Wow, thanks, it worked! Saved me alot!

Code: Select all

;-----------------------------------
;  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 ============

;
Krd
Posts: 405
Joined: 10 Mar 2020, 02:46

Re: Mouse And Keyboard Macro Recorder

22 Dec 2021, 08:26

@SpeedMaster

You didn't happen to know the answer of this above question too? :D

Anyone know how to change the mouse to Clicks instead of Mouseclick?

https://www.autohotkey.com/docs/commands/Click.htm

Click is just so much cleaner for the code than MouseClick, L. After all records I do this by find and replace, and removing empty lines which take twice the space.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 107 guests