Jump to content

Capture Mouse Path and Play it.


AlmarM
 Share

Recommended Posts

Hello,

I was a little bored today and I came up with this. Mayby it could be usefull for someone. ^_^

Note: (Human playback speed)

$fDelay = 50

$fSpeed = 1

Code

#include-once
#include <Misc.au3>

; #FUNCTION# =================================================================================================
; Name...........: _Mouse_Capture()
; Description ...: Captures the x and y pos for a given time.
; Syntax.........: _Mouse_Capture($fTime, $fDelay = 10)
; Parameters ....:  $fTime = For how long it should capture the movind mouse (Time in seconds).
;                   $fDelay = The capture delay (Default = 10).
; Errors ........: -
; Return values .: $_Return
; Author ........: Almar Mulder (AlmarM)
; Modified.......: -
; Remarks .......: -
; Related .......: _Mouse_Play()
; Link ..........: -
; Example .......: Yes
; #FUNCTION# =================================================================================================
Func _Mouse_Capture($fTime, $fDelay = 10)
    Local $_PosX, $_PosY
    Local $_fDown[2] = [0, 0]
    Local $_Return[2]
    
    $_Start = TimerInit()
    Do
        $_Stop = TimerDiff($_Start)
        $_GetPos = MouseGetPos()
        $_PosX &= $_GetPos[0] & ","
        $_PosY &= $_GetPos[1] & ","
        
        If _IsPressed("01") Then
            If $_fDown[0] = 0 Then
                $_PosX &= $_GetPos[0] & "ld,"
                $_PosY &= $_GetPos[1] & "ld,"
                $_fDown[0] = 1
            EndIf
        Else
            If $_fDown[0] = 1 Then
                $_PosX &= $_GetPos[0] & "lu,"
                $_PosY &= $_GetPos[1] & "lu,"
                $_fDown[0] = 0
            EndIf
        EndIf
        
        If _IsPressed("02") Then
            If $_fDown[1] = 0 Then
                $_PosX &= $_GetPos[0] & "rd,"
                $_PosY &= $_GetPos[1] & "rd,"
                $_fDown[1] = 1
            EndIf
        Else
            If $_fDown[1] = 1 Then
                $_PosX &= $_GetPos[0] & "ru,"
                $_PosY &= $_GetPos[1] & "ru,"
                $_fDown[1] = 0
            EndIf
        EndIf
        
        Sleep($fDelay)
    Until $_Stop >= $fTime * 1000
    
    $_Return[0] = $_PosX
    $_Return[1] = $_PosY
    Return $_Return
EndFunc

; #FUNCTION# =================================================================================================
; Name...........: _Mouse_Play()
; Description ...: Plays the captured mouse path.
; Syntax.........: _Mouse_Play($fCapture, $fSpeed = 1)
; Parameters ....:  $fCapture = The captured _Mouse_Capture variable.
;                   $fSpeed = The speed the mouse should move (Default = 1).
; Errors ........: -
; Return values .: -
; Author ........: Almar Mulder (AlmarM)
; Modified.......: -
; Remarks .......: -
; Related .......: _Mouse_Capture()
; Link ..........: -
; Example .......: Yes
; #FUNCTION# =================================================================================================
Func _Mouse_Play($fCapture, $fSpeed = 1)
    $_Trim_x = StringTrimRight($fCapture[0], 1)
    $_Trim_y = StringTrimRight($fCapture[1], 1)
    
    $_Split_x = StringSplit($_Trim_x, ",")
    $_Split_y = StringSplit($_Trim_y, ",")
    
    $_Max = $_Split_x[0]
    
    For $_i = 1 To $_Max
        MouseMove($_Split_x[$_i], $_Split_y[$_i], $fSpeed)
        If StringRight($_Split_x[$_i], 2) = "ld" Then MouseDown("left")
        If StringRight($_Split_x[$_i], 2) = "lu" Then MouseUp("left")
        If StringRight($_Split_x[$_i], 2) = "rd" Then MouseDown("right")
        If StringRight($_Split_x[$_i], 2) = "ru" Then MouseUp("right")
    Next
EndFunc

Example

$Var = _Mouse_Capture(5)
_Mouse_Play($Var)

Tell me what you think. ;)

Update:

- Added mouseclick capture

- Added mouseup and down capture - (WideBoyDixon)

AlmarM

Edited by AlmarM

Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

Link to comment
Share on other sites

Nice idea. Perhaps it would be better if it captured mouse clicks too?

^_^

WBD

Link to comment
Share on other sites

Very nice! Mouse clicks would be nice though.

[left][sub]We're trapped in the belly of this horrible machine.[/sub][sup]And the machine is bleeding to death...[/sup][sup][/sup][/left]

Link to comment
Share on other sites

Hmm yeah, mouse clicks. Ill try adding it. Thanks! ^_^

AlmarM

EDIT: Added mouseclicks. View first post!

Any more ideas?

Edited by AlmarM

Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

Link to comment
Share on other sites

Rather than MouseClick, perhaps you could use MouseDown/MouseUp so that I could capture and reply drag-and-drop?

WBD

Link to comment
Share on other sites

Rather than MouseClick, perhaps you could use MouseDown/MouseUp so that I could capture and reply drag-and-drop?

WBD

Do you have any ideas how to record the mousedown / up? And even playback?

AlmarM

Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

Link to comment
Share on other sites

Something like this?:

#include-once
#include <Misc.au3>

; #FUNCTION# =================================================================================================
; Name...........: _Mouse_Capture()
; Description ...: Captures the x and y pos for a given time.
; Syntax.........: _Mouse_Capture($fTime, $fDelay = 10)
; Parameters ....:  $fTime = For how long it should capture the movind mouse (Time in seconds).
;               $fDelay = The capture delay (Default = 10).
; Errors ........: -
; Return values .: $_Return
; Author ........: Almar Mulder (AlmarM)
; Modified.......: -
; Remarks .......: -
; Related .......: _Mouse_Play()
; Link ..........: -
; Example .......: Yes
; #FUNCTION# =================================================================================================
Func _Mouse_Capture($fTime, $fDelay = 10)
    Local $_PosX, $_PosY
    Local $_fDown[2] = [0, 0]
    Local $_Return[2]
    
    $_Start = TimerInit()
    Do
        $_Stop = TimerDiff($_Start)
        $_GetPos = MouseGetPos()
        $_PosX &= $_GetPos[0] & ","
        $_PosY &= $_GetPos[1] & ","
        
        If _IsPressed("01") Then
            If $_fDown[0] = 0 Then
                $_PosX &= $_GetPos[0] & "ld,"
                $_PosY &= $_GetPos[1] & "ld,"
                $_fDown[0] = 1
            EndIf
        Else
            If $_fDown[0] = 1 Then
                $_PosX &= $_GetPos[0] & "lu,"
                $_PosY &= $_GetPos[1] & "lu,"
                $_fDown[0] = 0
            EndIf
        EndIf
        
        If _IsPressed("02") Then
            If $_fDown[1] = 0 Then
                $_PosX &= $_GetPos[0] & "rd,"
                $_PosY &= $_GetPos[1] & "rd,"
                $_fDown[1] = 1
            EndIf
        Else
            If $_fDown[1] = 1 Then
                $_PosX &= $_GetPos[0] & "ru,"
                $_PosY &= $_GetPos[1] & "ru,"
                $_fDown[1] = 0
            EndIf
        EndIf
        
        Sleep($fDelay)
    Until $_Stop >= $fTime * 1000
    
    $_Return[0] = $_PosX
    $_Return[1] = $_PosY
    Return $_Return
EndFunc

; #FUNCTION# =================================================================================================
; Name...........: _Mouse_Play()
; Description ...: Plays the captured mouse path.
; Syntax.........: _Mouse_Play($fCapture, $fSpeed = 1)
; Parameters ....:  $fCapture = The captured _Mouse_Capture variable.
;               $fSpeed = The speed the mouse should move (Default = 1).
; Errors ........: -
; Return values .: -
; Author ........: Almar Mulder (AlmarM)
; Modified.......: -
; Remarks .......: -
; Related .......: _Mouse_Capture()
; Link ..........: -
; Example .......: Yes
; #FUNCTION# =================================================================================================
Func _Mouse_Play($fCapture, $fSpeed = 1)
    $_Trim_x = StringTrimRight($fCapture[0], 1)
    $_Trim_y = StringTrimRight($fCapture[1], 1)
    
    $_Split_x = StringSplit($_Trim_x, ",")
    $_Split_y = StringSplit($_Trim_y, ",")
    
    $_Max = $_Split_x[0]
    
    For $_i = 1 To $_Max
        MouseMove($_Split_x[$_i], $_Split_y[$_i], $fSpeed)
        If StringRight($_Split_x[$_i], 2) = "ld" Then MouseDown("left")
        If StringRight($_Split_x[$_i], 2) = "lu" Then MouseUp("left")
        If StringRight($_Split_x[$_i], 2) = "rd" Then MouseDown("right")
        If StringRight($_Split_x[$_i], 2) = "ru" Then MouseUp("right")
    Next
EndFunc

WBD

Link to comment
Share on other sites

One suggestion to avoid high cpu load when play back mouse movements:

add a Sleep(20) to the for...next loop in function Func _Mouse_Play($fCapture, $fSpeed = 1).

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

  • 4 weeks later...
  • 3 years later...

One suggestion to avoid high cpu load when play back mouse movements

Sleep wouldnt be alone able to reduce much the CPU load

Hence

a MouseHook would be more efficient

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

  • Moderators

PhoenixXL,

And you think it was worth necro-posting after 3 1/2 years to say that? :huh:

Guess what? I do not, so please think twice before posting next time. :naughty:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

techbard,

What part of my previous post was not clear? Do NOT necro-post again! :naughty:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...