Sign in to follow this  
someone2

How to simulate a mouse click

Recommended Posts

Hi. I am trying to write a program that can simulate a mouse click given (x,y) on the screen. I need the program to be independent of the kind of window open. If the user is just having his desktop shown, the click will cause an icon to be highlighted, or if a window is shown, the click will be sent to it.. Thanks a lot for all the help

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
i dont know you could see the SendMessage Function

Share this post


Link to post
Share on other sites
First, you need to get the handle of the window. Look up EnumWindows(), and enumerate all the windows. Use GetWindowRect() to obtain it's area, and check if it's rectangle has the x,y in it. If not, proceed to the next window.

If you run out of windows, that means the x,y is on the desktop, so use GetDesktopWindow() to get the desktop handle.

Now that you have the window handle, use SendMessage() with the x,y coordinates to that window.

All this hasn't been tested, but it should work. And it's assuming you're using C++. If using Visual Basic, C# or another language, you'll have to either find a way to call Win32 methods from that language, or look around in your language's documentation to find a way, if it's even possible.

Share this post


Link to post
Share on other sites

#include <windows.>
...
POINT curPos;
GetCurPos(&curPos);
int x = curPos.x;
int y = curPos.y;
mouse_event(MOUSEEVENTF_ABSOLUTE + MOUSEEVENTF_LEFTDOWN + MOUSEEVENTF_LEFTUP, x, y, 0, 0);


That should simulate a left mouse click at the position of the mouse. You can set it to click at any position though by changing the X and the Y.

Just look up mouse_event() on MSDN or something.

Share this post


Link to post
Share on other sites
mouse_event should be replaced with SendInput in win2k/xp.

Quote:
First, you need to get the handle of the window. Look up EnumWindows(), and enumerate all the windows. Use GetWindowRect() to obtain it's area, and check if it's rectangle has the x,y in it. If not, proceed to the next window.


WindowFromPoint is probably easier for that.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
This way clicks like the mouse on a certain position1, and it doesn't care if it's the desktop, explorer, a game or task manager (or anything else):
SetCursorPos(x, y);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);

Share this post


Link to post
Share on other sites
Thank you all for your replys.
SendInput() worked like a charm.. Thank you darookie.

I don't need to get Handle to windows or anything. It just actually moves the mouse pointer and makes it click. Wonderful stuff! :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this