• Advertisement
Sign in to follow this  

How to simulate a mouse click

This topic is 4906 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
doesn't sound like something that would have terribly many legitimate uses...

Share this post


Link to post
Share on other sites
Thanks for the ultra-fast reply.
Ummm.. in fact, it does! I need it. It is a long story, though... :)

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
SendMessage() won't work.
Use the SendInput function. This function will work for mouse as well as keyboard input. SendMessage() is not reliable.

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
on the extreme you could use directinput8, just set it to background and unexlusive mode.

im guessing you could probably do the same with sdl

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
Sign in to follow this  

  • Advertisement