[RESOLVED] Left Click, Sleep, Move Mouse, Sleep, etc...-VBForums
Results 1 to 7 of 7

Thread: [RESOLVED] Left Click, Sleep, Move Mouse, Sleep, etc...

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2012
    Posts
    12

    Resolved [RESOLVED] Left Click, Sleep, Move Mouse, Sleep, etc...

    I know this is probably basic but I'm having trouble figuring it out even with excessive amounts of googling...

    This is the code I am working with...

    HTML Code:
    Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
    Me.Cursor.Position = New Point(535, 195)
     Sleep(500)
     mouse_event(MOUSEEVENTF_LEFTDOWN, 335, 195, 0, 0)
     mouse_event(MOUSEEVENTF_LEFTUP, 335, 195, 0, 0)
     Sleep(5000)
     Me.Cursor.Position = New Point(390, 195)
     Sleep(500)
     mouse_event(MOUSEEVENTF_LEFTDOWN, 390, 195, 0, 0)
     mouse_event(MOUSEEVENTF_LEFTUP, 390, 195, 0, 0)
    End Sub
    The goal is to move cursor over a button in a webbrowser and click it, wait 5 seconds, move cursor over another button, click that button and so on...

    Everything is working, the cursor moves appropriately, the mouse clicks fine, but it doesn't do them sequentially. It does the following...

    - Moves mouse to first location (but doesn't click)
    - Waits about 7 seconds (not sure why, I would think it would be either 5.5 or 11 seconds...)
    - Moves to second mouse location and clicks mouse button twice in a row fast

    This is for a program just for me, this is the way I need to perform this, I could program it easily in autoit but would like to do it inside the vb code like this somehow.

    What am I doing wrong???

    Thanks for any help or points in the right direction!

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    87,793

    Re: Left Click, Sleep, Move Mouse, Sleep, etc...

    Don't use Sleep. If you want a pause between actions then use a Timer and perform your actions on the Tick event. If you need to change the interval between actions then Stop the Timer, change the Interval and then Start the Timer again.

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2012
    Posts
    12

    Re: Left Click, Sleep, Move Mouse, Sleep, etc...

    I'm confused about how the timer control works... Please help me...

    Assume these are the actions that I want to do with a pause in between them each...

    Move mouse to 1st position
    Code:
    Me.Cursor.Position = New Point(390, 195)
    Left mouse click
    Code:
    mouse_event(MOUSEEVENTF_LEFTDOWN, 390, 195, 0, 0)
    mouse_event(MOUSEEVENTF_LEFTUP, 390, 195, 0, 0)
    Move mouse to 2nd position
    Code:
    Me.Cursor.Position = New Point(623, 195)
    Left mouse click over that position
    Code:
    mouse_event(MOUSEEVENTF_LEFTDOWN, 623, 195, 0, 0)
    mouse_event(MOUSEEVENTF_LEFTUP, 623, 195, 0, 0)
    I am very new at vb, this is all I know to do so far, could you please help me fill in the gaps. I would greatly appreciate it!

    I put
    HTML Code:
    Timer1.Interval = 500
    Timer1.Enabled = True
    in the form load

    then, what would I put in this...

    Code:
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As 
    
    'what goes here?
    
    End Sub

  4. #4
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    24,021

    Re: Left Click, Sleep, Move Mouse, Sleep, etc...

    500 needs to be 5000 to get a 5 second pause. 500 is only half a second.

    What goes into the tick event handler is pretty much everything. You will need a form level variable of type integer. Start that variable at 0. In the tick event handler you would have something like this:
    Code:
    <your variable> += 1 'Add one to it.
    If <your variable> = 1 Then
      'Do the first move and click.
    ElseIf <your variable> = 2 Then
     'Do the second move and click.
    Else
     'Do the third move and click....and stop the timer.
    End If
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2012
    Posts
    12

    Re: Left Click, Sleep, Move Mouse, Sleep, etc...

    Ok, thanks guys!

    This is what I exactly what I needed, here is some code to leave for future searchers...

    Code:
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    
            'Advance tick variable by 1
            tickx = tickx + 1
            'Do first action
            If tickx = 2 Then
                Me.Cursor.Position = New Point(337, 195)
                'Second Action
            ElseIf tickx = 3 Then
                mouse_event(MOUSEEVENTF_LEFTDOWN, 390, 195, 0, 0)
                mouse_event(MOUSEEVENTF_LEFTUP, 390, 195, 0, 0)
                'Third
            ElseIf tickx = 4 Then
                Me.Cursor.Position = New Point(398, 195)
                'And last action
            ElseIf tickx = 5 Then
                mouse_event(MOUSEEVENTF_LEFTDOWN, 390, 195, 0, 0)
                mouse_event(MOUSEEVENTF_LEFTUP, 390, 195, 0, 0)
    
            End If
    
    
        End Sub

  6. #6

    Thread Starter
    New Member
    Join Date
    Apr 2012
    Posts
    12

    Re: Left Click, Sleep, Move Mouse, Sleep, etc...

    RESOLVED, not sure if there is a button to mark that or not...

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    87,793

    Re: Left Click, Sleep, Move Mouse, Sleep, etc...

    Quote Originally Posted by telepunk View Post
    RESOLVED, not sure if there is a button to mark that or not...
    Use the Thread Tools menu.

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.