Tie Jin
Tie Jin

Reputation: 1

SendInput not work consecutively

I've written a c++ snippet to simulate mouse click as following.

INPUT mouseData[2];
ZeroMemory(mouseData, sizeof(mouseData));

mouseData[0].type = INPUT_MOUSE;
mouseData[0].mi.dx = xx;
mouseData[0].mi.dy = yy;
mouseData[0].mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE | MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP;
mouseData[0].mi.mouseData = 0;
mouseData[0].mi.time = 0;

mouseData[1].type = INPUT_MOUSE;
mouseData[1].mi.dx = 10;
mouseData[1].mi.dy = 10;
mouseData[1].mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE | MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP;
mouseData[1].mi.mouseData = 0;
mouseData[1].mi.time = 0;

SendInput(1, &mouseData[0], sizeof(INPUT));
Sleep(30);
SendInput(1, &mouseData[1], sizeof(INPUT));

But it doesn't work well. Former SendInput works, but latter SendInput doesn't work. Why?

Another: I've tried SendInput as following

  1. call SendInput for mouse click
  2. Click the mouse manually on any position of screen.
  3. call SendInput

Above works well. But without 2, it doesn't work. why?

Upvotes: 0

Views: 1043

Answers (1)

Anders
Anders

Reputation: 101764

MSDN is not super helpful in its description of valid flags

The bits in this member can be any reasonable combination of the following values.

I personally would not say that it is reasonable to use both the down and up flags in the same input command. For maximum compatibility with all applications, perform the action like a normal user would: move, button down and finally button up.

I would recommend that you create a helper function that performs these three input steps if you are going to do a lot of mouse manipulation.

Upvotes: 2

Related Questions