Tech Support > Microsoft Windows > Development Resources > Using SendInput to Simulate Mouse Clicks
Using SendInput to Simulate Mouse Clicks
Posted by cppaddict on August 5th, 2004


Hi,

I am trying to use SendInput to Simulate a mouse click at position
(x,y) within a client window whose handle is stored in the class
member variable hWnd_.

I have built a successful implementation of the method using
mouse_event(), but it has the side effect of actually moving the mouse
on the screen, which I don't want. Also, since it will be used in a
multi-threaded environment, I am worried that another thread could
possibly move the mouse in betwen the down and up events created by
the successive calls to mouse_event.

So I am trying a SendInput implementation instead. The code below
compiles but does nothing.

Can anyone tell me what's wrong with it?

Thanks for any help,
cpp

<code>
void NativeOcr::simulateMouseClick(int x, int y) {
RECT windowRect;
GetWindowRect(hWnd_,&windowRect);
MOUSEINPUT miDown = { windowRect.left + x, windowRect.top + y,
0, MOUSEEVENTF_LEFTDOWN, 0, 0 };
INPUT inDown;
inDown.type = INPUT_MOUSE;
inDown.mi = miDown;
MOUSEINPUT miUp = { windowRect.left + x, windowRect.top + y,
0, MOUSEEVENTF_LEFTUP, 0, 0 };
INPUT inUp;
inUp.type = INPUT_MOUSE;
inUp.mi = miUp;
INPUT inputs[2];
inputs[0] = inDown;
inputs[1] = inUp;
SendInput(2,inputs,sizeof inUp);
}
</code>


Similar Posts