Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

code to click, move, double click, mouse?

Posted on 2004-10-03
6
Medium Priority
?
494 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 86

Accepted Solution

by:
Mike Tomlinson earned 300 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 86

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 86

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
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 …
Screencast - Getting to Know the Pipeline

885 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