Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

move mouse programatacally

Status
Not open for further replies.

NW

Programmer
Feb 3, 2000
61
0
0
GB
Hi All,
Is there a way to move mouse pointer to a specific location (i.e to a button on a different application) and execute its click event programmatically?

I have an external application that run music every time when I click the next button on it. What I want is to create a program that run (say in every 5 mins) to click on this next button programmatically.

I hope this is possible in VB?
Thanks in advance.
NW
 

Yes it is possible to click on another applications button, but you have to use the SendMessage API to send the button click. Look in help or at MS or you can search this site for examples on how to use the SendMessage API.

Good Luck

 
I want to discuss what I've learned about the mouse_event subroutine, and I think this might be of some use to many here in the 5&6 forum.

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)

dx and dy are mouse coordinates, not twips or pixels or points... there are something like 65,000 units on X and 65,000 units on Y. I don't recall the exact number of units.

ok, so here are some example calls:

mouse_event MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_MOVE, 200, 200,0,0

This call moves the mouse to 200,200 (the cButtons and dwExtraInfo are not used as far as I can tell)

Remember, the x and y coordinates are mouse_coordinates, not twips or points... You can do some conversions between this coordinate system and twips but I don't have the exact figures on them so well worry about that later on. I'll find that sooner or later and update you on this.

Here's how to do the clicks:

mouse_event MOUSEEVENTF_ABSOLUTE or MOUSEVENTF_LEFTDOWN, 200,200,0,0

Sends a mouse down (first half of a click)

and as you might guess, the second half of the click is done with:

mouse_event MOUSEEVENTF_ABSOLUTE Or MOUSEEVENTF_LEFTUP, 200,200,0,0

Now here's something else to chew on... if you Omit the "MOUSEEVENTF_ABSOULTE Or" then you are no longer telling the mouse the absolute position to do the function... it's now a relative position, so 200, 200 for dx and dy would now be 200 mouse X units from its current X position, and 200 Y units from it's current Y position.

Experiment with this and you'll get an understanding of simulating moving and clicking the mouse.

Remember, your dx and dy grid is 65,000 some odd units each direction, so your dx and dy values are LONG

Three simple calls let you move to a point, mouse_down and mouse_up. You can omit the "move" and just send the mouse_down and Mouse_up in a spot to do it quicker if you need too.

More to come... Merry Christmas!


Tuna - It's fat free until you add the Mayo!
 
Oh, you'll need the constants...

Private Const MOUSEEVENTF_ABSOLUTE = &H8000 ' absolute move
Private Const MOUSEEVENTF_LEFTDOWN = &H2 ' left button down
Private Const MOUSEEVENTF_LEFTUP = &H4 ' left button up

Reference you WINAPI32.TXT file for others...

Tuna - It's fat free until you add the Mayo!
 
Ignore that last part of what I said about ... you should go ahead and do the MOVE before the Mouse_down and Mouse_up. if you don't do the move first and just send the down then up, you'll get a click wherever the mouse is sitting. The coordinates don't seem to matter unless you are doing the move! I thought I had done this before, but it seems I was wrong about that. Sorry folks.

and I omitted the constant for the move, like a sleepy tuna would do at nearly 1 am:

Private Const MOUSEEVENTF_MOVE = &H1 ' mouse move

Tuna - It's fat free until you add the Mayo!
 
MSDN tells of the dwExtraInfo being used in Windows 2000 and XP... mouse wheel... I'll report on that soon too.

Tuna - It's fat free until you add the Mayo!
 

Tuna,

I must be slipping, I forgot all about that API I am glad that you pointed it out. I can also tell you why I forgot about it.

If I remember correctly when using this API you will need to know where the button is located. Its physical left, top, width, and height properties, and since it is a child window the values that are returned from enuming child windows and getting their positions will be the left and top values based upon its container. So then you need to know its containers left and top, but what if its container is a frame contained in a picture box in an mdi child that is not maximized?

So there was my problem. In all of this I found the GetWindowText API and if I knew the text of the button that I wanted to click on I could use the SendMessage API to send that button a mouse click without having to enumerate the appropriate child windows of the main parent window to find the left and top of that command button so I could click it.

Now for my statement above. I state, "but you have to use the SendMessage API to send the button click", which is wrong as Tuna has pointed out and I am sure that there are other people in this forum that could come up with other solutions or combinations of them or even prove me incorrect in my memory of the mouse API.

So Tuna I have to say that, "you da fish!" I am glad that you brought that back to my memory because I can use that for something else (training user how to use program with automated learning program).

(BTW the skin is not fat free!) :)

May everyone have a happy and safe holiday season.

Good Luck

 
Hey VB5er... In reading what I wrote last night, I must have been alot more sleepy than I realized. I wish I could re-write it now and make sure it makes a bit more sense...

It's a fun little API call though isn't it! I first used it doing some automation on a tedious daily task that a sweet little lady at my old workplace had to do. It was basically an antiquated application she had to run EVERY Morning to transfer data from one rediculous sytem to an even more rediculous system. Took her 25 minutes every day and the steps were always identical. I promised to automate it for her in return for a twinky.

She gave me a box of twinkies every week for a month... She did that same job every morning for 5 years, now the job is set up on a timer and she need only verify that it completed itself before 9 am.

Anyway, I hope this gets to NW so he/she can make use of it. I also use it to kill a nag screen that pops up after I close one of my applications. The nag screen has a button you must click to close it, but the button doesn't get focus (i suspect it's not really a button because I cant get it's attention except by clicking that sucker!)

The coordinate system is 65,535 x 65,535 I think... does that sound right? I used to have some code written to convert the mouse coordinates to twips for easy button location. I've got a MAJOR headache today and I'm not going to attempt it. Maybe later.

Who eats the skin of a tuna anyway? sheesh!



Tuna - It's fat free until you add the Mayo!
 
Bump... NW, did you get this? Tuna - It's fat free until you add the Mayo!
 
Oh no, no,no - we're not going to start bumping in here, are we?
 
Why, is there a problem with bumping? I just want to be sure the person who asked for help with this gets the help they asked for... Tuna - It's fat free until you add the Mayo!
 
I will resuse this off-line. I have been struggling to get at record volume settings and selects - Creative Recorder does not offer keyboard shortcuts (little else does) for these so SendKeys are useless for it,
but a mouse prodder.......
 
The person that asked the orginal question has this thread marked for their attention (as do two other people), which means they get automatically emailed when answers are given

It's just a personal opinion, but I hate bumping.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top