Jump to content

_MouseCopy Function


erifash
 Share

Recommended Posts

Hey, I just created a useful function for copying the mouse movement into an AutoIt script. Here it is:

Func _MouseCopy()
  While 1
    $pos = MouseGetPos()
    If $last <> $pos Then
      $line = "MouseMove(" & $pos[0] & ", " & $pos[1] & ", 2)"
      FileWriteLine("_MouseCopied.au3", $line)
    EndIf
    $last = $pos
  Wend
EndFunc

Although it is a little jerky and it dosen't stop writing to the file when the mouse is idle (see variable $last), I think it is pretty good so far.

Oh, yeah, here is how I used it:

HotKeySet("^!e", "EndLoop")
HotkeySet("^!s", "_MouseCopy")

$last = -1

While 1
  Sleep(10)
Wend

Func _MouseCopy()
  While 1
    $pos = MouseGetPos()
    If $last <> $pos Then
      $line = "MouseMove(" & $pos[0] & ", " & $pos[1] & ", 2)"
      FileWriteLine("_MouseCopied.au3", $line)
    EndIf
    $last = $pos
  Wend
EndFunc

Func EndLoop()
  Exit 0
EndFunc

So if anyone thinks it's cool or you have comments or suggestions please reply!

Link to comment
Share on other sites

same script as above with visual cue...

Nice! White dots...

I didn't see them at first because I was testing it in the folder that the script was in but when I brought it out to the side I noticed little white dots!

Thanks! Great script.

Link to comment
Share on other sites

One question though...

Why are the hex RBG values reversed in the DLLCall?

e.g.: to get red then 0x0000FF, or blue 0x00FF00, or green 0xFF0000, instead of red 0xFF0000, blue 0x00FF00, or green 0x0000FF.

Link to comment
Share on other sites

Okay, thanks to ezzetabi's _IsPressed function I can now copy the mouse clicks on both the left and right buttons! _MouseCopy can now technically be classified as a "mouselogger". I also added a pause function. Here's the code:

HotKeySet("+`", "EndLoop")
HotKeySet("+1", "Pause")
HotkeySet("`", "_MouseCopy")

Dim $last[2]
$last[0] = -1
$last[1] = -1

While 1
  Sleep(10)
Wend

Func _MouseCopy()
$line = "HotKeySet(""`"", ""EndMove"")" & @CRLF & @CRLF
FileWriteLine("_MouseCopied.au3", $line)
  While 1
    $pos = MouseGetPos()
    If $last <> $pos Then
      $line = "MouseMove(" & $pos[0] & ", " & $pos[1] & ", 2)"
      FileWriteLine("_MouseCopied.au3", $line)
    EndIf
    If _IsPressed('01') = 1 Then
      $line = "MouseClick(""left"", " & $pos[0] & ", " & $pos[1] & ", 1, 2)"
      FileWriteLine("_MouseCopied.au3", $line)
    EndIf
    If _IsPressed('02') = 1 Then
      $line = "MouseClick(""right"", " & $pos[0] & ", " & $pos[1] & ", 1, 2)"
      FileWriteLine("_MouseCopied.au3", $line)
    EndIf
    $last = $pos
  Wend
EndFunc

Func _IsPressed($hexKey)
; $hexKey must be the value of one of the keys.
; _IsPressed will return 0 if the key is not pressed, 1 if it is.
  
  Local $aR, $bRv;$hexKey
  $hexKey = '0x' & $hexKey
  $aR = DllCall("user32", "int", "GetAsyncKeyState", "int", $hexKey)
;If $aR[0] = -32767 Then
  If $aR[0] <> 0 Then
     $bRv = 1
  Else
     $bRv = 0
  EndIf
  
  Return $bRv
EndFunc ;==>_IsPressed

Func Pause()
  While 1
    Sleep(10)
  Wend
EndFunc

Func EndLoop()
  $line = @CRLF & "Func EndMove()" & @CRLF & "  Exit 0" & @CRLF & "EndFunc"
  FileWriteLine("_MouseCopied.au3", $line)
  Exit 0
EndFunc

I hope this comes in handy for someone.

Also...

Hi Nakuribon!

Link to comment
Share on other sites

That looks really good, and I hate to pick at it but just a suggestion...

You may consider taking the logging algohithm out of _MouseCopy and make a seperate function just for that. I know it doesn't matter right? Well actually if you take it out your code will be more modular and you could build both functions independently to be used in all of the scripts you need the functionality in.

It is a personal pet pieve, but overall it looks really good!

*** Matt @ MPCS

Link to comment
Share on other sites

Thanks, and no I am not offended. I don't exactly understand what you just said though. Could you please clarify on what I should do?

Link to comment
Share on other sites

I just looked over your code again, and you do things very odd. Are you sure it doesn't give you an error when you run it? The reason I ask is all of your $line lines are formatted illegally (i believe so anyway could be wrong). Here is an example:

This is what you do:

$line = "HotKeySet(""`"", ""EndMove"")" & @CRLF & @CRLF

And this is what in this language makes sense to do:

$line = 'HotKeySet("`", "EndMove")' & @CRLF & @CRLF

You also use two loops instead of one. You have one for the application (so it doesn't close when logging is disabled) and a second for when you are doing the logging (actually a third to pause). This could all be accomplished with the single application loop (the first one) using variable flags. This is not as critical to change but is just another way to solve the same problem.

What I was saying is that you could create a logging Function that looked something like this (bare minimum) (untested):

Func LogLine( $line )
   FileWriteLine("_MouseCopied.au3", $line)
EndFunc

And then replace the "$line=" and "FileWriteLine()" lines with (example):

LogLine('HotKeySet("`", "EndMove")' & @CRLF & @CRLF)

Just a couple thoughts, good luck!

*** Matt @ MPCS

Link to comment
Share on other sites

Okay, yes it does not come up with any errors. You use two sets of quotes in order to format the string the right way. Also did you mean something like this?

HotKeySet("+`", "EndLoop")
HotKeySet("+1", "Pause")
HotkeySet("`", "_MouseCopy")

Dim $last[2]
$last[0] = -1
$last[1] = -1

While 1
  Sleep(10)
Wend

Func _MouseCopy()
LogLine("HotKeySet(""`"", ""EndMove"")" & @CRLF & @CRLF)
  While 1
    $pos = MouseGetPos()
    If $last <> $pos Then LogLine("MouseMove(" & $pos[0] & ", " & $pos[1] & ", 2)")
    If _IsPressed('01') = 1 Then LogLine("MouseClick(""left"", " & $pos[0] & ", " & $pos[1] & ", 1, 2)")
    If _IsPressed('02') = 1 Then LogLine("MouseClick(""right"", " & $pos[0] & ", " & $pos[1] & ", 1, 2)")
    $last = $pos
  Wend
EndFunc

Func _IsPressed($hexKey)
; $hexKey must be the value of one of the keys.
; _IsPressed will return 0 if the key is not pressed, 1 if it is.
  
  Local $aR, $bRv;$hexKey
  $hexKey = '0x' & $hexKey
  $aR = DllCall("user32", "int", "GetAsyncKeyState", "int", $hexKey)
;If $aR[0] = -32767 Then
  If $aR[0] <> 0 Then
     $bRv = 1
  Else
     $bRv = 0
  EndIf
  
  Return $bRv
EndFunc ;==>_IsPressed

Func LogLine($line)
  FileWriteLine("_MouseCopied.au3", $line)
EndFunc

Func Pause()
  While 1
    Sleep(10)
  Wend
EndFunc

Func EndLoop()
  LogLine(@CRLF & "Func EndMove()" & @CRLF & "  Exit 0" & @CRLF & "EndFunc")
  Exit 0
EndFunc

Thanks, that does shorten the code a bit!

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...