Solved

code to click, move, double click, mouse?

Posted on 2004-10-03
6
478 Views
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?
0
Comment
Question by:djiangr
  • 3
  • 2
6 Comments
 
LVL 85

Accepted Solution

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

Regards,

Idle_Mind


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 Const MOUSEEVENTF_LEFTDOWN = &H2
Private Const MOUSEEVENTF_LEFTUP = &H4

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
0
 

Author Comment

by:djiangr
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?

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

~IM
0
 

Author Comment

by:djiangr
ID: 12223087
thanks,
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)

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

~IM
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now