Results 1 to 9 of 9

Thread: Smooth Mouse Movement

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2000
    Posts
    5
    I need some help trying to figure this thing out. Right now I can get the mouse to move from Point A to Point B. For instance, my program picks a random X value and Y value and then the mouse goes to that point. However, I don't want the mouse to just go from one coordinate to the next I want it to smoothly move there as if someone was actually controlling it. Now I have a record mouse movement feature which uses a timer with a really short interval that gets all the coordinates the person moves and all the clicks, things like that. The only problem with that is that nothing is random about that. I want to be able to pick a random number and move it there as if it was a recorded mouse movement. Does this make any sense to anyone other than me? LoL, I think I did a bad job of explaining this. If this doesn't make sense let me know and I will see if I can explain it better.

    Thanks,

    Publius

  2. #2
    Hyperactive Member
    Join Date
    Sep 1999
    Location
    Leeds, UK
    Posts
    287

    Wink A loop should do it

    Hi,

    Well to perform a task like this I would use a loop. Lets say the mouses current X pos is at 88 and you would like it to be at 245:

    Do until CurrentPos = TargetPos
    CurrentPos = CurrentPos + 1
    SetCursorPos CurrentPos
    loop

    I hope you will see what I'm doing here and there for be able to perform your task.

    Good Luck!

    [Edited by rino_2 on 05-14-2000 at 04:45 AM]
    rino_2
    Visual Basic, HTML
    Please Visit my Site: Richard's VB Site

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2000
    Posts
    5

    Thought About The Loop...

    Yeah I thought about the loop, but then I figured it wouldn't work right. I don't really know why I figured that, I just kinda did. Ok, I don't think that works, I just tried it and it turns into an infinite loop for some reason. Also what happens if your Current Position is like 120 and the new X it picks is like 12? Then you have

    Do Until 120 = 12

    That's not going to work. When I plugged your code in the mouse just shoots the end of the screen and locks up, lol. I'll play around with it and see what I get. In the mean time I have to work on my research paper.

    Thanks for the post,

    Publius

  4. #4
    Guest

    Post

    Well the loop would work, but you may have to put it in an if then, or case form. That way if current co-ordinate is less than the random generated co-ordinate, then the loop would add numbers if the other way around then the loop would minus numbers untill it got to the random position, also one more thing, the co-ordinates are 2d, XY, You probably knew this, since u got like 25% working, but you'll have to make structred loops for example: Psuedo code:
    for mousexpos = currentxpos to randomxpos step -1 (or 1)
    for mouseypos = currentypos to randomypos step -1 (or 1)
    SetCursorPos mousexpos,mouseypos
    next mouseypos
    next mousexpos

    This is off the top of my head, try doing this thing with the "For Loop", it may take longer, but it's more controled this way, and the "step -1" or "Step 1" is when you figure out if u want to add to the co-ordinates, or subtract from the co-ordinates... I dont' know, you figure out the rest

  5. #5
    Lively Member
    Join Date
    Apr 2000
    Location
    Hell
    Posts
    89
    Actually, I've used this type of lewp for smooth motion...it's not the least bit random, but actually mimics how the mouse usually moves, because the mouse accelerates when you're moving it.

    Incidentally, a journal hook would be MUCH better for you to work with...

    The code I had used wasn't quite smooth enough, so I made it more complicated Going vertically, it usually goes like a rocket, but other than that it's pretty damn good.

    Oh, and it breaks if the user moves the mouse while its working.

    Code:
    Private Type Point
      X As Long
      Y As Long
    End Type
    
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As Point) As Long
    Private Declare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long
    
    Private Sub Sleep(dur As Single)
      Dim newT As Single
      newT = Timer + dur
      While (Timer < newT)
        DoEvents
      Wend
    End Sub
    
    Private Function Sign(ByVal X As Single) As Long
      If (X = 0) Then Sign = 0 Else Sign = X / Abs(X)
    End Function
    
    Private Function MaxAbs(ByVal X As Long, ByVal Y As Long) As Long
     If (Abs(X) < Abs(Y)) Then MaxAbs = Y Else MaxAbs = X
    End Function
    
    Private Function PtBetween(ByVal X As Long, ByVal x0 As Long, ByVal x1 As Long, ByVal x2 As Long) As Boolean
      ' x0, x1, and x2 should be either x0<x1><x2 or x0>x1>x2
      If (x0 < x2) Then
        PtBetween = (X > x1)
      Else
        PtBetween = (X < x1)
      End If
    End Function
    
    Private Sub SmoothMotion(ByVal toX As Long, ByVal toY As Long)
      ' toX and toY are in SCREEN COORDINATES, in PIXELS
      Dim Pt As Point, OrgPt As Point
      Dim ptX As Single, ptY As Single, halfx As Single, halfy As Single
      Dim Divisor As Single
      
      Divisor = 2 ' change this to change the accel
      
      GetCursorPos Pt
      OrgPt = Pt
      halfx = (toX + OrgPt.X) / 2
      halfy = (toX + OrgPt.Y) / 2
      ptX = Pt.X + Sign(toX - halfx)
      ptY = Pt.Y + Sign(toY - halfy)
      While Abs(toX - ptX) > 3 Or Abs(toY - ptY) > 3
        If PtBetween(ptX, OrgPt.X, halfx, toX) Then
          ptX = ptX + (toX - ptX) / Divisor
        Else
          ptX = ptX + (ptX - OrgPt.X) / Divisor
        End If
        If PtBetween(ptY, OrgPt.Y, halfy, toY) Then
          ptY = ptY + (toY - ptY) / Divisor
        Else
          ptY = ptY + (ptY - OrgPt.Y) / Divisor
        End If
        SetCursorPos ptX, ptY
        Sleep 0.05
      Wend
      SetCursorPos toX, toY
    End Sub
    - Steve

    Real programmers use COPY CON PROGRAM.EXE

  6. #6
    New Member
    Join Date
    May 2000
    Posts
    1

    Mouse Click

    Now that we know how to move the mouse, how do we make VB to click for us? is there any API for that? or we should use SendMessage?

    thanks for the reply

  7. #7
    Guest
    Well, See if you can find my post, where I posted how to change the caption of another program/folder or whatever. What you do is do the same thing, use "FindWindow" (and an API SPY), to find the Window's Handle, and then Send a message to it, using SendMessage, but the message will be WM_KEYDOWN or something like that... I'm not at home right now, if you need more info, just reply, and when I get home, I'll just post some source code too...

  8. #8
    Lively Member
    Join Date
    Apr 2000
    Location
    Hell
    Posts
    89
    This is why using a journal playback hook is much better.

    Use WindowFromPoint() to get the window handle, then SendMessage WM_LBUTTONDOWN and WM_LBUTTONUP as necessary.
    - Steve

    Real programmers use COPY CON PROGRAM.EXE

  9. #9
    Hyperactive Member
    Join Date
    Nov 1999
    Posts
    363
    You can slow down your loop by using Sleep. To suspend for say 1/10 of a second, use:
    Code:
    Private Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
    
    Sleep(100)
    Wade

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width