Automatic mouse manipulation to specific XY coordinates

In VB5 -- I am working on a project that currently reads the XY coordinates from the windows desktop and displays them in two different labels.  I have two text boxes to retrieve from the user the newly found XY coordinates.  Now when they press 'GO' I want to have the mouse cursor move to the specified coordinates and then do some action, like double clicking. Could you please help me?
LVL 3
grooneAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dirtdartCommented:
Declare Function SetCursorPos Lib "user32" Alias "SetCursorPos" (ByVal x As Long, ByVal y As Long) As Long


The declare above will set the mouse to the specified position.  If it is performing an action that occurs within your program when it gets there, then the easiest way is to just call the subroutine for that action.  If not, then you'll have to use the following:

Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

where hWnd is the hWnd of the form receiving the message, wMsg is the message you want to send (WM_MBUTTONDOWN, etc), and wParam and lParam can be set to zero and ignored.
0
lmarceauCommented:
The Mouse_Event() API function is able to activate the clickings, the moving, and anything the user can do with the mouse. Much more simple than having to code more lines.

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

As for the correct usage, you've to find out coz' I haven't used them before. Save your work before meddling with APIs.
0
ngpuddingCommented:
Okay, I suppose I need someone to hold my hand...but heck for 200 points...any way, Dirts declaration was good. I had just about that same function declaration, and there was an error in lmarceau's declarations - PackedPt = Pt.y * 2^^16 + Pt.x
And thank you ngpudding.

[yes or no question] Is the SetCursorPos used in the same aspect as to what I have below

I think I am confused to how the function and declarations are used in this case.  The code I am using to get the coordinates is as follows:
=======
Option Explicit

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

Private Type POINTAPI
        X As Long
        Y As Long
End Type
Private gStop As Boolean
----------------

Private Sub Form_Load()

Dim mousePT As POINTAPI
Dim X As Long
Dim Y As Long

  gStop = False

  Do
    If gStop = True Then Exit Do
    Call GetCursorPos(mousePT)
    X = mousePT.X
    Y = mousePT.Y
    Form2.Label1 = X
    Form2.Label1 = Y
    DoEvents
  Loop
End Sub
-------------
Private Sub form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  gStop = True
End Sub
======
so in retrospect, the SetCursorPos should be used in the same method where X = Text1 and Y = Text2?  Give me a good answer Dirt and you will get an "A"
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

lmarceauCommented:
Dirdart should get credit for this one...  Use the following for your declare...

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

Create a project, place a single command button on the form and in its click event place the following code

SetCursorPos x, y

This will demonstrate how to use it.

0
grooneAuthor Commented:
Exactly.  You've got the hang of it.  Just take the values contained in Text1 and Text2 (you might consider changing them to Single with CSng (X = CSng(Text1.Text))) and use SetCursorPos

X = CSng(Text1.Text)
Y = CSng(Text2.Text)

SetCursorPos X, Y

That should do it.
0
mrmickCommented:
Next time, don’t reject an answer when you think it's on track just because you're having trouble... just comment to that affect.
0
dirtdartCommented:
Sorry about rejecting it Dirt, this being my first time around, wasn't entirely sure how to do it.  Thank you all for the information, each of you gave information that will come in useful.  Well, since I am not expert material, I better go buy some more points -- a just in case type of thing you know.

One final question if I may....At this point, I can then use
the
PostMessage hwnd, WM_LBUTTONDOWN, MK_LBUTTON, PackedPt
method as described by lmarceau to cause the click of the mouse..correct?

Again!  Thanks everyone, I finally found a place for some answers!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mrmickCommented:
Yes, you can use the PostMessage from lmarceau or the SendMessage that I described to you.  Either one will perform the event you are looking for.  I know this subject in and out, because I once made a Tutorial for Surfing our corporate intranet.  It would automatically move the cursor to the next link (except I did mine incrementally, so it looked like the mouse was actually moving), and automatically click on the link to send the user to the next page of the tutorial.  If you have any questions, email me at dirtdart01@yahoo.com.  I'll do what I can for you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.