How Do I Use SendInput in Delphi?

2020-05-29 06:26发布

问题:

I was using the Mouse_Event Function in Delphi 2009, but the Delphi documentation says this function has been superceded and to use SendInput instead.

The Delphi SendInput documentation defines the syntax and parameters, but there are no examples and it is not clear how to use the function. I've looked around on the web, and can't find any good Delphi examples.

Specifically, I am trying to simulate the left mouse down and then up. Currently I do this with Mouse_Event as follows:

    Mouse_Event(MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
    Mouse_Event(MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);

How would I do this using SendInput?


Followup:

I ended up leaving my code as is as @David suggested.

But I gave @opc0de the answer since he did give an answer to my question. However, I can't vouch that it is correct, because I never did try it.

回答1:

Here is how to simulate a left click for more details visit http://msdn.microsoft.com/en-us/library/ms646310(VS.85).aspx

var
eu: array [0..1] of TInput;
begin
  ZeroMemory(@eu,sizeof(eu));
  eu[0].Itype := INPUT_MOUSE;
  eu[0].mi.dwFlags :=MOUSEEVENTF_LEFTDOWN;
  eu[1].Itype := INPUT_MOUSE;
  eu[1].mi.dwFlags :=MOUSEEVENTF_LEFTUP;
  SendInput(2,eu[0],sizeof(TInput));
end;

And here is for simulating right click

var
eu: array [0..1] of TInput;
begin
  ZeroMemory(@eu,sizeof(eu));
  eu[0].Itype := INPUT_MOUSE;
  eu[0].mi.dwFlags :=MOUSEEVENTF_RIGHTDOWN;
  eu[1].Itype := INPUT_MOUSE;
  eu[1].mi.dwFlags :=MOUSEEVENTF_RIGHTUP;
  SendInput(2,eu[0],sizeof(TInput));
end;