How to simulate a mouse click

Started by
9 comments, last by someone2 19 years, 8 months ago
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
Advertisement
doesn't sound like something that would have terribly many legitimate uses...
"Game Programming" in an of itself does not exist. We learn to program and then use that knowledge to make games.
Thanks for the ultra-fast reply.
Ummm.. in fact, it does! I need it. It is a long story, though... :)
i dont know you could see the SendMessage Function
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.
SendMessage() won't work.
Use the SendInput function. This function will work for mouse as well as keyboard input. SendMessage() is not reliable.
#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.
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.
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
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);

This topic is closed to new replies.

Advertisement