检测键盘或鼠标事件是否由软件触发 [英] Detect if keyboard or mouse events are triggered by a software
问题描述
在Windows上运行的桌面应用程序中,是否有办法确定键盘或鼠标事件是从硬件而不是从TeamViewer,Steam或其他远程桌面软件之类的应用程序触发的?
Is there a way to determine whether the keyboard or mouse events are triggered from a hardware rather than an application like TeamViewer, Steam or some other remote desktop software in a desktop application running on Windows?
我的目的不是阻止机器人,而是阻止对应用程序的远程访问.
My purpose is not to prevent bots, but to prevent remote access to the application.
似乎RawInput API可以让我检测使用SendInput API发送的虚假事件.正确吗?
It seems that RawInput API lets me detect fake events sent using SendInput API. Is it correct?
推荐答案
The low-level keyboard/mouse hooks provided by SetWindowsHookEx()
report if input was generated by actual devices or injected by application code.
对于低级键盘钩,该钩子提供了指向 KBDLLHOOKSTRUCT
结构的指针,该成员的flags
成员包含用于假输入的LLKHF_INJECTED
标志.
For a low-level keyboard hook, the hook provides a pointer to a KBDLLHOOKSTRUCT
structure, which has a flags
member that contains a LLKHF_INJECTED
flag for fake input.
对于低级鼠标钩,该钩子提供了指向 MSLLHOOKSTRUCT
结构的指针,该成员具有flags
成员,其中包含用于伪造输入的LLMHF_INJECTED
或LLMHF_LOWER_IL_INJECTED
标志.
For a low-level mouse hook, the hook provides a pointer to a MSLLHOOKSTRUCT
structure, which has a flags
member that contains either a LLMHF_INJECTED
or LLMHF_LOWER_IL_INJECTED
flag for fake input.
每个钩子都可以返回非零值,以阻止输入传递到钩子链的其余部分,进而传递到目标窗口.
Either hook can return a non-zero value to block the input from being passed to the rest of the hook chain, and consequently to the target window.
根据( GetRawInputDeviceInfo()
函数:
Regarding the Raw Input API, according to (an older version of 1) the documentation for the GetRawInputDeviceInfo()
function:
hDevice [in,可选]
类型:HANDLE
hDevice [in, optional]
Type: HANDLE
原始输入设备的句柄.这来自WM_INPUT消息的lParam,来自RAWINPUTHEADER的hDevice成员或来自GetRawInputDeviceList. 如果应用程序通过使用SendInput插入输入数据,也可以为NULL.
A handle to the raw input device. This comes from the lParam of the WM_INPUT message, from the hDevice member of RAWINPUTHEADER, or from GetRawInputDeviceList. It can also be NULL if an application inserts input data, for example, by using SendInput.
1 :突出显示的注释已在当前版本的文档中删除,我不知道为什么.
1: the highlighted note has been removed in the current version of the documentation, I do not know why.
因此, <对于假输入,c10> 消息将为NULL.
So, the hDevice
that is reported by a WM_INPUT
message will be NULL for fake input.
但是,无法使用Raw Input API阻止输入.您仍然需要一个低级钩子.
however, it is not possible to block input with the Raw Input API. You still need a low-level hook for that.
这篇关于检测键盘或鼠标事件是否由软件触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!