Adblock breaks this site

Moving Mouse to X,Y Position

Discussion in 'Programming General' started by Agoz, Sep 16, 2009.

  1. Agoz

    Agoz Newcomer

    Joined:
    Sep 13, 2009
    Posts:
    5
    Referrals:
    0
    Sythe Gold:
    0
    Moving Mouse to X,Y Position

    Hello this is my second thread! ;)

    This time I'm wondering if there are any tutorials or how to make the mouse itself or a "second" mouse to move to a position on screen? And how would I do to make it random move and click somewhere between a "rectangle" on screen? :huh:

    Thanks for help.

    Agoz :D
     
  2. Hey321

    Hey321 Forum Addict

    Joined:
    Jul 30, 2007
    Posts:
    503
    Referrals:
    0
    Sythe Gold:
    0
    Moving Mouse to X,Y Position

    Code:
            Windows.Forms.Cursor.Position = New System.Drawing.Point(X, Y)
    
    Will move it to the position inserted into the System.Drawing.Point. To click in a rectangle, do some math and find a random point inside your rectangle, then use a variable or two, or even just do the math in the function.

    ~Sandstorm
     
  3. ipivb

    ipivb Newcomer

    Joined:
    Sep 17, 2009
    Posts:
    9
    Referrals:
    0
    Sythe Gold:
    0
    Moving Mouse to X,Y Position

    here is some code to preform ma mouse click and also move the mouse
    Code:
        Public Declare Auto Function SetCursorPos Lib "User32.dll" (ByVal X As Integer, ByVal Y As Integer) As Integer
        Public Declare Auto Function GetCursorPos Lib "User32.dll" (ByRef lpPoint As Point) As Integer
        Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Integer)
        Public Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Integer, ByVal dx As Integer, ByVal dy As Integer, ByVal cButtons As Integer, ByVal dwExtraInfo As Integer)
        Public Const MOUSEEVENTF_LEFTDOWN = &H2 ' left button down
        Public Const MOUSEEVENTF_LEFTUP = &H4 ' left button up
        Public Const MOUSEEVENTF_MIDDLEDOWN = &H20 ' middle button down
        Public Const MOUSEEVENTF_MIDDLEUP = &H40 ' middle button up
        Public Const MOUSEEVENTF_RIGHTDOWN = &H8 ' right button down
        Public Const MOUSEEVENTF_RIGHTUP = &H10 ' right button up
        Public Sub MouseLeftClick()
            mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
            Sleep(50)
            mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
        End Sub
    
     Cursor.Position = New Point(100, 100)
    
    
            SetCursorPos(330, 288)
            MouseLeftClick()
    here is some code to put the mouse x,y in a textbox
    Code:
       Dim mouseLocation As Point = Windows.Forms.Cursor.Position
    
            TextBox1.Text = "X: " & mouseLocation.X.ToString
    
            TextBox2.Text = "Y: " & mouseLocation.Y.ToString
     
  4. Agoz

    Agoz Newcomer

    Joined:
    Sep 13, 2009
    Posts:
    5
    Referrals:
    0
    Sythe Gold:
    0
    Moving Mouse to X,Y Position

    Thanks but I wasn't clear enough sorry, I mean like how to make it a "second mouse" and make it MOVE to that position like a real mouse not "teleporting" Like some bot clients do...

    Thanks
    /Agoz :D
     
  5. war833

    war833 Member

    Joined:
    Dec 11, 2008
    Posts:
    82
    Referrals:
    0
    Sythe Gold:
    0
    Moving Mouse to X,Y Position

    Use a loop that moves it pixel by pixel. Maybe not the best way, but it's an idea.
     
  6. super_

    super_ Member

    Joined:
    Dec 20, 2008
    Posts:
    91
    Referrals:
    0
    Sythe Gold:
    0
    Moving Mouse to X,Y Position

    SendMessage.
    Oh how hard.
    PS ipivb: mouse_event is old and ugly. Use SendInput instead.
    Code:
        INPUT input;
        input.mi.dx = <move x>;
        input.mi.dy = <move y>;
        input.mi.mouseData = 0;
        input.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
        input.mi.time = 0;
        input.type = INPUT_MOUSE;
        SendInput(1, &input, sizeof(INPUT));
    
    Above is C code, just convert it to VB.
     
  7. Coron

    Coron Forum Addict
    Banned

    Joined:
    Aug 9, 2009
    Posts:
    287
    Referrals:
    0
    Sythe Gold:
    0
    Moving Mouse to X,Y Position

    Something I quickly wrote, works smoothly :p This was the only method I could think of, it's pretty simple and all.

    Code:
     
    Private Declare Sub mouse_event Lib "user32.dll" ( _
        ByVal dwFlags As Int32, _
        ByVal dx As Int32, _
        ByVal dy As Int32, _
        ByVal cButtons As Int32, _
        ByVal dwExtraInfo As Int32 _
        )
    
        Private Declare Function SetCursorPos Lib "user32.dll" ( _
        ByVal X As Int32, _
        ByVal Y As Int32 _
        ) As Boolean
    
        Public Sub moveMouse(ByVal TargetX As Integer, ByVal targetY As Integer)
            Dim X As Integer = MousePosition.X, y As Integer = MousePosition.Y
    
            Do Until X = TargetX And y = targetY
                If (X > TargetX) Then
                    X = X - 1
                End If
                If (X < TargetX) Then
                    X = X + 1
                End If
                If (y > targetY) Then
                    y = y - 1
                End If
                If (y < targetY) Then
                    y = y + 1
                End If
                SetCursorPos(X + New Random().Next(3, 4), y + New Random().Next(3, 4))
                Threading.Thread.Sleep(10)
            Loop
        End Sub
    

    Usage: moveMouse(x, y)

    Planning to use this? Please credit Coron <[email protected]> :)
     
  8. Agoz

    Agoz Newcomer

    Joined:
    Sep 13, 2009
    Posts:
    5
    Referrals:
    0
    Sythe Gold:
    0
    Moving Mouse to X,Y Position

    How would that code be like if I would like it to move the cursor to 245, 565 ?
     
  9. Coron

    Coron Forum Addict
    Banned

    Joined:
    Aug 9, 2009
    Posts:
    287
    Referrals:
    0
    Sythe Gold:
    0
    Moving Mouse to X,Y Position

    moveMouse(245, 565)
     
< VIRUS in VB08 real and dangerous | HELP: Java Indepentant study for school >


 
 
Adblock breaks this site