code to click, move, double click, mouse?

Posted on 2004-10-03
Last Modified: 2011-10-03
is there a way to move a mouse to a certain point on the screen and click/double click it there through code with VB6?
also, how can i get the coordinates of the mouse at a certain point?
Question by:djiangr
  • 3
  • 2
LVL 85

Accepted Solution

Mike Tomlinson earned 75 total points
ID: 12214105
Create a new project and add a Label and a Timer.  The code below will show you the current position of the cursor.  To simulate a mouse click at some point on the screen just use the MouseClick() function passing in the X,Y coordinates of the point you want clicked on.  To simulate a double click just call MouseClick() twice in a row with the same coordinates.



Option Explicit

Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long

Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, _
    ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, _
    ByVal dwExtraInfo As Long)

Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, _
    ByVal y As Long) As Long


Private Sub Form_Load()
    Timer1.Interval = 50
End Sub

Private Sub Timer1_Timer()
    Dim z As POINTAPI
    GetCursorPos z
    Label1.Caption = z.x & ", " & z.y
End Sub

Public Sub MouseClick(x As Long, y As Long)
    SetCursorPos x, y
    mouse_event MOUSEEVENTF_LEFTDOWN, x, y, 0, 0
    mouse_event MOUSEEVENTF_LEFTUP, x, y, 0, 0
End Sub

Author Comment

ID: 12217739
would this code work outside the form?  say i wnated to make it click something on the desktop, would that work?

i want tto make somethign that will move the mouse to certain points on the screen and click it

will it work if the program is minimized?

LVL 85

Expert Comment

by:Mike Tomlinson
ID: 12217779
>> would this code work outside the form?  say i wnated to make it click something on the desktop, would that work?

Yes, it will work anywhere on the screen.

>> i want tto make somethign that will move the mouse to certain points on the screen and click it

The MouseClick() sub will move the cursor and then click on the point passed in.

>> will it work if the program is minimized?

To make it work while minimized you would need to either use a timer or possibly register a HotKey to make the code run.  Explain a little further how you want your app to work and I can suggest a more concrete solution.


Author Comment

ID: 12223087
i wanted to make a thing where you can set at a certain time when to move the mouse click where, etc.
for example, it could shut down the computer at a certain time or something like that...(i know there are programs to do this, but just an example)

LVL 85

Expert Comment

by:Mike Tomlinson
ID: 12223351
Sure, that's possible via the use of the built in Timer control.  You just poll the current time with the timer control and when the target time has been reached, call the MouseClick() sub with the desire coordinates.


Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Specific format 21 197
MacOS and programming in React 7 35
Apps blocked by Java 9 79
Query Syntax 17 35
This is an explanation of a simple data model to help parse a JSON feed
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question