?
Solved

Automatic mouse manipulation to specific XY coordinates

Posted on 1997-12-18
8
Medium Priority
?
368 Views
Last Modified: 2008-02-01
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?
0
Comment
Question by:groone
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 5

Expert Comment

by:dirtdart
ID: 1445825
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
 
LVL 1

Expert Comment

by:lmarceau
ID: 1445826
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
 
LVL 1

Expert Comment

by:ngpudding
ID: 1445827
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:lmarceau
ID: 1445828
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
 
LVL 3

Author Comment

by:groone
ID: 1445829
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
 
LVL 8

Expert Comment

by:mrmick
ID: 1445830
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
 
LVL 5

Accepted Solution

by:
dirtdart earned 800 total points
ID: 1445831
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
 
LVL 8

Expert Comment

by:mrmick
ID: 1445832
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

752 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