4

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.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
lkessler
  • 19,819
  • 36
  • 132
  • 203
  • You don't need a delphi example. Find one in a different language. The calls to the API will be the same. You also don't need to change your existing code, it still works fine. – David Heffernan Jul 03 '11 at 07:09
  • To make myself 100% clear, if your code using `Mouse_Event` meets your needs there is no good reason to change it to use `SendInput`. – David Heffernan Jul 03 '11 at 07:32
  • The [`mouse_event`](http://msdn.microsoft.com/en-us/library/ms646260(VS.85).aspx) and [`SendInput`](http://msdn.microsoft.com/en-us/library/ms646310(VS.85).aspx) functions are ***not*** part of the Delphi RTL. They are part of the Microsoft Windows operating system API. That is, they are the same independent of what programming language you use to write your Windows applications. So you are most likely wrong when you say that the "Delphi documentation says..." and "The Delphi SendInput documentation...". Embarcadero doesn't document the WinAPI functions. Microsoft does. – Andreas Rejbrand Jul 03 '11 at 11:29
  • @David: My understanding is that if code is deprecated, you should change it when possible since it is not guaranteed that all current or future configurations of Windows will support it. – lkessler Jul 03 '11 at 15:07
  • That understanding is incorrect. The API is marked as having been superseded. It's not been deprecated. There's a big difference. Microsoft don't remove widely used user mode API's. You are trying to fix that which is not broken. – David Heffernan Jul 03 '11 at 15:44
  • Thank you, @David, on the clarification of the terms. That makes sense. – lkessler Jul 03 '11 at 16:06
  • Note that SendInput is more robust than the old APIs because it supports atomic input sequences. Depending on the context, this vulnerability/sensitivity might be considered "broken" and worth the code change risk to fix. – Mattias Andersson Mar 08 '13 at 19:35

1 Answers1

7

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;
opc0de
  • 11,557
  • 14
  • 94
  • 187
  • What are $0001 and $0002 for mouseData of MouseInput, why do you need them? – Sertac Akyuz Jul 03 '11 at 12:04
  • @Sertac: `const XBUTTON1 = 1; XBUTTON2 = 2`. It tells which mouse button should be depressed/released, left (1) or right (2). http://msdn.microsoft.com/en-us/library/ms646273(VS.85).aspx – Andreas Rejbrand Jul 03 '11 at 12:13
  • @Andreas - I see, thanks. Then they're not needed, XBUTTONs are not left/right buttons. – Sertac Akyuz Jul 03 '11 at 12:39
  • @Sertac: Oh, sorry, I drew premature conclusions. The `mouseData` should be zero: "If dwFlags does not contain MOUSEEVENTF_WHEEL, MOUSEEVENTF_XDOWN, or MOUSEEVENTF_XUP, then mouseData should be zero." (I did a common mistake: I assumed the answerer was doing things right!) Of course, the OS knows what button we are talking about since we specify `MOUSEEVENTF_LEFTDOWN` or `MOUSEEVENTF_RIGHTDOWN` as `dwFlags`. – Andreas Rejbrand Jul 03 '11 at 12:42
  • Isn't it nice of Microsoft to replace a simple to use function with this mess? And @David wondered why I needed a Delphi code example. Sorry, it's not so obvious to people who don't often work with the Windows API. I'll try it out and check that it works for me. – lkessler Jul 03 '11 at 15:09
  • @lkessler Since `mouse_event` is so much simpler and you've already coded it you really shouldn't bother changing. You'll only run the risk of introducing a bug. – David Heffernan Jul 03 '11 at 15:48
  • Not so fast... The documentation notes at least one valuable benefit that SendInput provides over mouse_event and keybd_event: atomicity--and the robustness that this adds could well warrant the risk of switching. From http://msdn.microsoft.com/en-us/library/ms646310%28v=vs.85%29.aspx : "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." – Mattias Andersson Mar 08 '13 at 19:32