问题
Is there any way to figure out where did a mouse event come from? I mean, if I code a C/C++ program on Windows, and get a mouse click event on it, how can I find if this event come from a mouse driver, a touchpad, or if it was send by an application (mouse event simulation by sending appropriate message like WM_LBUTTONDOWN).
Thanks for any help :)
回答1:
This is not possible for an application in user mode - mouse events generally don't provide documented info on event source. There is the way to obtain some message extra info by Win32 API function GetMessageExtraInfo but there is no safe way to interpret this data. It is very device specific, undocumented and never guaranteed to ever present.
To solve this task you need to develop your own Mouse Filter driver basing on Windows DDK sample.
Its callback has input parameter MOUSE_INPUT_DATA - structure containing mouse event info. There is the field UnitId
:
UnitId Specifies the unit number of the mouse device. A mouse device name has the format
\Device\PointerPortN
, where the suffix N is the unit number of the device. For example, a device, whose name is\Device\PointerPort0
, has a unit number of zero, and a device, whose name is\Device\PointerPort1
, has a unit number of one.
回答2:
GetAsyncKeyState function can be used to check if the button was pressed, and unfortunately SendInput cannot trick this function. So you can simulate a mouse click, but the program can check if the button was really pressed.
So creating your own mouse driver is better. I needed a safe way so simulate mouse/keyboard behavior for my bot, and I wrote a detailled article on my blog http://poker-botting.blogspot.fr/2012/11/how-to-simulate-mouse-and-keyboard.html
来源:https://stackoverflow.com/questions/12622312/how-to-find-where-a-mouse-event-come-from