How we can make mouse click event in a certain position without moving mouse(I mean that make computer think a position is clicked with mouse) with C++
2 Answers
The SendInput
function in windows API will get you started. Have a look at the following link for the definition of the method,its input parameters and return values :
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310(v=vs.85).aspx
You can follow the links in the page to know more about the structures and data-types used in the function.
Update :
You can start with something like this
#include<Windows.h>
int main()
{
INPUT input;
input.type=INPUT_MOUSE;
input.mi.dx=0;
input.mi.dy=0;
input.mi.dwFlags=(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MOVE|MOUSEEVENTF_RIGHTDOWN|MOUSEEVENTF_RIGHTUP);
input.mi.mouseData=0;
input.mi.dwExtraInfo=NULL;
input.mi.time=0;
SendInput(1,&input,sizeof(INPUT));
return 0;
}
This will automatically move your mouse to the top left corner of the screen and make a right-click. Now, if you mean to make a click somewhere on the screen without moving your mouse, I think that is not possible using SendInput(). You do not need to worry about moving the mouse as your program will do it by itself. That is what the 'MOUSEEVENTF_MOVE' flag tells the program to do. If you do not use the flag, then the click will take place at the current position of your mouse.

- 561
- 2
- 9
-
I used PostMessage(hwnd,WM_LBUTTONDOWN,MK_LBUTTON,MAKELPARAM(p.x, p.y)); PostMessage(hwnd,WM_LBUTTONUP,MK_LBUTTON,MAKELPARAM(p.x, p.y)); but it clicks where the mouse is it doesn't click the position i told to click – Zoli Sep 11 '12 at 07:26
-
the link you said about inputs i saw but i couldn't find how to say click at a special position – Zoli Sep 11 '12 at 07:27
-
mouse_event(MOUSEEVENTF_LEFTDOWN,813,731, 0, 0); mouse_event(MOUSEEVENTF_LEFTUP,813,731, 0, 0); it clicks where the mouse is it doesn't click (813,731) – Zoli Sep 11 '12 at 11:00
-
@ZiDoM : `mouse_event()` method is deprecated. You should avoid using it. Did you follow the links to the INPUT structure and then the MOUSEINPUT structure ? I am afraid you will have to dig and do some reading on the page I posted link to. Try and you will succeed :) – sandyiscool Sep 11 '12 at 15:53
-
postmessage handles 1 of the messages. the position is used for the move message, the mouse down, then the mouse up. Clicking at a specified point requires 3 messages. – Dan Apr 25 '19 at 22:57
-
no. You cannot simply combine move, button down, and button up. Which one should go first? Split this into 3 input packets. – Dan May 02 '19 at 16:36
You can use SendInput()
function to emulate mouse clicks and keyboard strokes:
The SendInput function inserts the events in the INPUT structures serially into the keyboard or mouse input stream. These events are not interspersed with other keyboard or mouse input events inserted either by the user (with the keyboard or mouse) or by calls to keybd_event, mouse_event, or other calls to SendInput.
You can also use SendMessage()
or PostMessage()
to send the button-press message, e.g.
SendMessage(hWnd, WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM(mousePosX, mousePosY));
, but it is less reliable. Note that in this way you may also need to send WM_LBUTTONUP
later, depending on the way you handle events in your application.
Also check this question, the accepted answer is pretty detailed.

- 1
- 1

- 29,228
- 8
- 68
- 105
-
-
How should i tell the position with INPUT structures without moving the mouse – Zoli Sep 11 '12 at 06:45
-
@ZiDoM, well, you probably know _where do you want to click_, don't you? – SingerOfTheFall Sep 11 '12 at 06:59
-
-
1
-
@ZiDoM : You do not need to move the mouse to a point on the screen and then call SendInput() to perform the click. I guess you can estimate the co-ordinates of the point at which you want to perform the click. If you want to click at topmost left corner, then the co-ordinates are x=0,y=0. If your screen resolution is 1280x1024,for example, then for performing a click at the center of the screen, the co-ordinates will be x=1280/2,y=1024/2. You do not need to move your mouse to the center for performing this click. Calling SendInput() with the proper parameters does that for you automatically. – sandyiscool Sep 11 '12 at 07:30
-
i know the co-ordinates and i can send input but i don't know how to send input in a certain position – Zoli Sep 11 '12 at 08:35
-
1you cannot. You must get the mouse current position, move the mouse to the click location, click the mouse, move the mouse back to the original location. And nobody is any wiser. – Dan May 02 '19 at 16:38