AutoHotkey Community

It is currently May 24th, 2012, 9:40 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 30 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: AHK MouseRecorder (MoRe)
PostPosted: February 8th, 2007, 4:12 pm 
Offline

Joined: March 24th, 2005, 11:50 am
Posts: 398
Location: germany
AHK MouseRecorder (MoRe)
*19.04.09 Edited a copy-Paste error in source-Code, thanks Trikster

Today I want to present a funny little tool, the mouse recorder. I hope it is the first mouserecorder and nobody scripted such a tool in AHK, before;-)
Last week i read the thread about the new advanced AHK ScriptWriter, it will be the most important tool in Autohotkey, because it is the first thing a new user will test. If the script writer works well we will get lots of new members in our community. Inspired by the scriptwriter I thought about a Mouse-movement-recorder, which records and replays realtime, so you
can watch the mouse moving, as you did move your mouse in the record mode. At the moment, Mousewheel, left and right button and the movement is recorded. Mouseclicks and wheel are send normal, no controlsend implemented right now. Coordinates are sent relative to the desktop (screen-mode).

The script starts recording by pressing F1, and stops with F2. Replay starts with F3. There is no Gui till now, I wrote the script to test the possibilities ;-)
Each 15ms mouse position and keystates of the buttons + wheel are checked. If any Parameter changes, the new parameters are written to a textfile, including the time since start of recording, and the ID of the Window under the mouse.
In replay mode, each 15ms is checked, if the time from a counter reaches the time of the recorded next Dataset. If so, the Dataset is send to the mouse, and the mouse is moved accordingly. At the beginning of the Replay, the same window is activated, as it was active at the beginning of the recording.

I tested it with MS_Paint, I drew a little picture, and replayed it, you can see the two testimages, below.

Image


Missing features:
a nice Gui
scalable replay-speed
recording of additional mouse-Buttons (xbutton1 and xbutton2)
recording of a Joystick
recording of keybord-buttons
export-function to build a standalone AHKscript from the recorded mousemovements
usage of controlsend
support screen and relative Coordmode
documentation in the code

Code:
;***********************************************
;**                                           **
;**  Mouse movement recorder (MoRe) 0.1       **
;**  based on the idea of AHK script writer   **
;**  written by garath                        **
;**                                           **
;***********************************************

#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
#Persistent
CoordMode, Mouse, Screen
SetBatchLines, -1


;*************************************+*********
;**                                           **
;**           F1 Start recording              **
;**           F2 Stop  recording              **
;**           F3 Replay                       **
;**                                           **
;***********************************************


~WheelDown::Wheel_down += A_EventInfo
~Wheelup::Wheel_up += A_EventInfo


f1::
  FileDelete, Mausbewegungen.txt
  Mouse_moves =
  Wheel_up =
  Wheel_down =
  Time_old := A_TickCount
  SetTimer, WatchMouse, Off
  SetTimer, WatchMouse, 10
  SetTimer, WatchMouse, on
Return

f2::
  SetTimer, WatchMouse, Off
  ToolTip
Return

f3::Gosub, replay

Return


;******************************************

WatchMouse:
  Time_Index := A_TickCount - Time_old
  MouseGetPos, xpos, ypos, id, Control
  GetKeyState, lButt, LButton
  GetKeyState, mButt, MButton
  GetKeyState, rButt, RButton
  Mouse_Data = %xpos%|%ypos%|%lButt%|%mButt%|%rButt%|%Wheel_up%|%Wheel_down%|%ID%|%Time_Index%`n
  ToolTip, recording
  If (xpos<>xpos_old OR ypos<>ypos_old OR Wheel_up OR Wheel_down OR lButt<>Lbutt_old OR mButt<>mButt_old OR rButt<>rButt_old)
    {
      FileAppend, %Mouse_data%, Mausbewegungen.txt
      xpos_old  := xpos
      ypos_old  := ypos
      lButt_old := lButt
      mButt_old := mButt
      rButt_old := rButt
      Wheel_up =
      Wheel_down =
    }
Return

;*******************************************

replay:
  FileRead,Mouse_moves, Mausbewegungen.txt
  StringReplace, Mouse_data, Mouse_moves, `n, @, All
  StringSplit, Mouse_data_, Mouse_data , @
  Loop, %Mouse_data_0%
      StringSplit, Mouse_data_%A_Index%_, Mouse_data_%A_Index% ,|
  Data_Index = 1
  Data_Index_old := 1
  id := Mouse_data_1_8
  WinActivate, ahk_id %id%
  Time_old := A_TickCount
  SetTimer, Replaytimer, Off
  SetTimer, Replaytimer, 10
  SetTimer, Replaytimer, on
Return

;********************************************

replaytimer:
  Time_Index := A_TickCount - Time_old
  Mouse_data_%Data_Index%_9 += 0

  If (Time_Index > Mouse_data_%Data_Index%_9)
    {
      MouseMove, Mouse_data_%Data_Index%_1, Mouse_data_%Data_Index%_2
      lButt := Mouse_data_%Data_Index%_3
      mButt := Mouse_data_%Data_Index%_4
      rButt := Mouse_data_%Data_Index%_5
      wheel_up := Mouse_data_%Data_Index%_6
      wheel_down := Mouse_data_%Data_Index%_7

      If (Mouse_data_%Data_Index_old%_3 <> Mouse_data_%Data_Index%_3)
          MouseClick , Left ,,,,, %lButt%
      If (Mouse_data_%Data_Index_old%_4 <> Mouse_data_%Data_Index%_4)
          MouseClick , middle ,,,,, %mButt%
      If (Mouse_data_%Data_Index_old%_5 <> Mouse_data_%Data_Index%_5)
          MouseClick , Right ,,,,, %rButt%
      If (Mouse_data_%Data_Index%_6)
          MouseClick, WheelUp, , , %wheel_up%       
      If (Mouse_data_%Data_Index%_7)
          MouseClick, Wheeldown, , , %wheel_down%       

      Data_Index_old := Data_Index
      Data_Index += 1
      If (Data_Index = Mouse_data_0)
        {
          SetTimer Replaytimer, Off
          Data_Index = 0
        }
    }
;  x := Mouse_data_%Data_Index%_9
;  tooltip %Time_Index%_%A_TickCount% - %Time_old% __%x%

Return



Maybe, next week I can present the "MoRe Joy" ;-)

**garath walks away, searching his 10 Years old analog Jostick ;-)**


Last edited by garath on April 18th, 2009, 1:21 pm, edited 2 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 8th, 2007, 4:15 pm 
So give us some MoRe, dude ! :lol:


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 8th, 2007, 4:25 pm 
wow......


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 8th, 2007, 5:07 pm 
Offline

Joined: March 24th, 2005, 11:50 am
Posts: 398
Location: germany
Thanks, Bobo and guest 8)

Btw, long time ago I saw somebody using a transparent window to catch mouse events, I would like to use OnMessage, because of performance, the timer performs well, but it seems not to be the best way to catch the mousemovements and clicks. Anybody knows, where I can find it in the Forum? I searched for it the last days, but didnt find it:-(


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 8th, 2007, 6:25 pm 
You can use OnMessage with trans win. U have example in the help file, how to make win transparent.


Keywords: WinSet, WM_MOUSEXXX messages, OnMessage


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 8th, 2007, 9:40 pm 
Offline

Joined: March 24th, 2005, 11:50 am
Posts: 398
Location: germany
@majkinetor !
I tried to get WM_Mouse messages, but I only got them with non-transparent AHK-Windows, will do some further research the next days.
I tried it with the OnScreenDisplay example, but only got Messages hovering over the visible text, not over the transparent window :-( If I remove transparecy, I get the messages :-/


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 9th, 2007, 3:08 am 
Offline

Joined: April 21st, 2006, 11:25 pm
Posts: 56
Location: East Coast, USA
garath wrote:
Btw, long time ago I saw somebody using a transparent window to catch mouse events


I tried it, maybe you mean this thread?

http://www.autohotkey.com/forum/viewtop ... highlight=

I never did get it to work in AHK very well.

_________________
____________________


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 16th, 2007, 12:10 pm 
When I had this problem, I created a .dll with MASM32 to solve it. The license agreement on that is pretty restrictive... so I just kept it for personal use.

I don't know if you are still working on this, but I finally transfered the code over into RosAsm, which is under the GPL license. Since I can now do with it what I want, so can you :D

The DLL installs a low-level mouse hook and uses PostMessage to notify the script that a mouse event has occured. It doesn't care what mouse event... any will do. The script has to do all the work. Also, it doesn't pass any parameters to the script. So it is best to just set up hotkeys for the buttons and use the DLL to notify of X/Y movement (which is the only thing it was created for).

The DLL is written in Assembly Language and for what it does it is pretty much optimized.

Code in the DLL:
Code:
[hInstance: ?    hHook : ?    hWnd : ?]

Proc Main:
    Arguments @Instance, @Reason, @Reserved

        If D@Reason = &DLL_PROCESS_ATTACH
           push D@Instance
           pop D$hInstance
        Else_If D@Reason = &DLL_PROCESS_DETACH
            ; ...

        Else_If D@Reason = &DLL_THREAD_ATTACH
            ; ...

        Else_If D@Reason = &DLL_THREAD_DETACH
            ; ...

        End_If

        mov eax &TRUE
Endp




Proc MouseProc::
    Arguments @nCode, @wParam, @lParam

    push D@lParam
    push d@wParam
    push d@nCode
    push D$hHook
    call 'user32.CallNextHookEx'

    push 3000h
    push D$hWnd
    call 'user32.PostMessageA'

    xor eax eax

EndP

Proc HookMouse::
    Arguments @hWnd

    push D@hWnd
    pop D$hWnd

    push &NULL
    push D$hInstance
    push MouseProc
    push &WH_MOUSE_LL
    call 'user32.SetWindowsHookExA'
    push eax
    pop D$hHook

EndP

Proc UnhookMouse::

    push D$hHook
    call 'user32.UnhookWindowsHookEx'

    xor eax eax

EndP


Sample script:
Code:
OnExit, Exit

PID := DllCall("GetCurrentProcessId")
  If ErrorLevel
    MsgBox %ErrorLevel%

DllCall("Hook.dll\HookMouse", "UInt", PID)
  If ErrorLevel
    MsgBox %ErrorLevel%

OnMessage(0x3000,"DoMouse")

Return



DoMouse()
{
  MouseGetPos, x, y
  TrayTip,, %x% : %y%
  Return
}


Esc::ExitApp

Exit:
DllCall("Hook.dll\UnhookMouse")
ExitApp
Return


DLL is here: Hook.DLL

You can download RosAsm to see the code inside :)

A note...

You don't have to use GetCurrentProcessId for the PostMessage handle. You can use GetCurrentThread, GetCurrentThreadId, or if your script has some kind of GUI, use the Window hWnd for it...etc etc.

Hope you (or at least someone) finds it useful :)


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 16th, 2007, 5:36 pm 
Offline

Joined: March 24th, 2005, 11:50 am
Posts: 398
Location: germany
I am working on it, from time to time.
Thanks for the hook, will have a look on it, this weekend. :-)
Right now, I have implemented a gui, scalable replay-speed from 10%-2500%, and a Keyboard-recorder, I only have to secure, that no skript-kiddie can use it as K_e_y-L_o_G_g_e_r :-(


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 21st, 2007, 12:07 am 
Doesn´t work for me at all....


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 31st, 2007, 10:52 am 
Mouse movement recorder works well with me ( XP SP1 ).

It is awesome.

Just what I do need.


Thank you very match for sharing !!!
---

What I do want to point out to new people who did not get it work is that.

When you copy it to clipboard and paste it to notepad there was this line warped in two lines.

If (xpos<>xpos_old OR ypos<>ypos_old OR Wheel_up OR Wheel_down OR lButt<>Lbutt_old OR mButt<>mButt_old OR rButt<>rButt_old)


It need to be in one line like that.

So remove some spaces and save.

Hopefully that will help someone.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 31st, 2007, 11:14 am 
Offline

Joined: May 24th, 2006, 2:49 pm
Posts: 4511
Location: Belgrade
Quote:
I tried it with the OnScreenDisplay example, but only got Messages hovering over the visible text, not over the transparent window If I remove transparecy, I get the messages :-/

Its true.
You can monitor screen mouse movement. When mouse is inside rectangle of your window, you do your job.

Zippo() wrote:
Hope you (or at least someone) finds it useful

Very nice. thx

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 4th, 2007, 10:20 am 
Offline

Joined: December 14th, 2005, 3:08 pm
Posts: 219
Any chance that it could be modified to repeat the same mouse movement a set number of times?

I tried
Code:
f3::
loop, 5
{
Gosub, replay
}
Return


but it doesn't make it repeat.

_________________
Stuart Halliday


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 1st, 2008, 5:18 pm 
Bump?


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 7th, 2008, 2:53 pm 
loop, 2
{
Send {F3}
Sleep, 1883000
}

Create a second script that looks like this. Set the time for however long your recording takes to replay. Set the loop for however many times you want it to replay.

Open the MoRe script, then open the second script when you want to start. Your second script wil start the MoRe script and repeat it for howver many time you specifiy.

I also added a pause funtion to the original script because I found I could not stop it once it started replaying and I had to unplug my computer :S

f2::
SetTimer, WatchMouse, Off
ToolTip
Return

f3::Gosub, replay
F4::Pause


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 30 posts ]  Go to page 1, 2  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: lblb and 20 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group