Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 497
  • Last Modified:

code to click, move, double click, mouse?

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
djiangr
Asked:
djiangr
  • 3
  • 2
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
djiangrAuthor Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
>> 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
 
djiangrAuthor Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now