I would like to simulate mouse events using the Win32 API; how can I do it?
我想使用Win32 API模拟鼠标事件;我该怎么做?
What I want to do is simulate the event at the most basic level, the level at which the system has just the event type and the co-ordinates and hasn't yet figured which window it must relay it to.
我想要做的是在最基本的级别模拟事件,系统只有事件类型和坐标的级别,还没有确定它必须转发到哪个窗口。
I don't know if that's how things work. Either way, I need help doing it. Would I have to meddle at the driver level?!
我不知道这是怎么回事。无论哪种方式,我都需要帮助。我是否必须干涉司机级别?!
To make my requirements clear, I don't want to target any window, I just want the system to think the mouse was clicked or moved by the user. And I would be coding in C.
为了明确我的要求,我不希望任何窗口,我只是希望系统认为鼠标被用户点击或移动。而我将用C编码。
2 个解决方案
#1
22
You're looking for the SendInput
function, which allows you to synthesize mouse movements and button clicks in your code by specifying an array of INPUT
structures corresponding to input events.
您正在寻找SendInput函数,它允许您通过指定与输入事件对应的INPUT结构数组来合成代码中的鼠标移动和按钮单击。
UINT WINAPI SendInput(
__in UINT nInputs, // number of structures in the pInputs array
__in LPINPUT pInputs, // an array of INPUT structures, representing an event
__in int cbSize // the size, in bytes, of an INPUT structure
);
Note, however, that this function is subject to User Interface Privilege Isolation (UIPI), which means that your application is only permitted to inject input to applications that are running at an equal or lesser integrity level.
但请注意,此功能受用户界面权限隔离(UIPI)的约束,这意味着您的应用程序仅允许将输入注入到以相同或较低完整性级别运行的应用程序。
#2
6
Use mouse_event
(winuser.h). The following code will move the mouse then perform a click at the new location. You can do this in two lines but this is more verbose.
使用mouse_event(winuser.h)。以下代码将移动鼠标,然后在新位置执行单击。您可以在两行中执行此操作,但这更详细。
Note that X and Y are specified in mickeys, 0 to 65535. This is then mapped onto the current resolution, i.e. 0,0 will be the top left corner and 65535,65535 will be the lower right hand corner.
请注意,X和Y在mickeys中指定,0到65535.然后将其映射到当前分辨率,即0,0将是左上角,65535,65535将是右下角。
mouse_event(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE, x, y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
#1
22
You're looking for the SendInput
function, which allows you to synthesize mouse movements and button clicks in your code by specifying an array of INPUT
structures corresponding to input events.
您正在寻找SendInput函数,它允许您通过指定与输入事件对应的INPUT结构数组来合成代码中的鼠标移动和按钮单击。
UINT WINAPI SendInput(
__in UINT nInputs, // number of structures in the pInputs array
__in LPINPUT pInputs, // an array of INPUT structures, representing an event
__in int cbSize // the size, in bytes, of an INPUT structure
);
Note, however, that this function is subject to User Interface Privilege Isolation (UIPI), which means that your application is only permitted to inject input to applications that are running at an equal or lesser integrity level.
但请注意,此功能受用户界面权限隔离(UIPI)的约束,这意味着您的应用程序仅允许将输入注入到以相同或较低完整性级别运行的应用程序。
#2
6
Use mouse_event
(winuser.h). The following code will move the mouse then perform a click at the new location. You can do this in two lines but this is more verbose.
使用mouse_event(winuser.h)。以下代码将移动鼠标,然后在新位置执行单击。您可以在两行中执行此操作,但这更详细。
Note that X and Y are specified in mickeys, 0 to 65535. This is then mapped onto the current resolution, i.e. 0,0 will be the top left corner and 65535,65535 will be the lower right hand corner.
请注意,X和Y在mickeys中指定,0到65535.然后将其映射到当前分辨率,即0,0将是左上角,65535,65535将是右下角。
mouse_event(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE, x, y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);