检测模拟键盘/鼠标输入
有没有办法检测 Windows 上的模拟键盘/鼠标输入。例如,用户在键盘上与 sendKeys/PostMessage/屏幕键盘上键入内容。有什么方法可以区分两者吗?
编辑:也许一个例子会有所帮助。我正在制作一个游戏,想要区分真实输入与合成键盘/鼠标消息的 WinAPI。
Is there a way to detect simulated keyboard/mouse input on Windows. For example, a user types something on his keyboard vs sendKeys/PostMessage/On-screen keyboard. Is there a way that I can distinguish between the two?
EDIT: Perhaps an example would help. I am making a game and want to distinguish between real input vs WinAPI synthesizing keyboard/mouse messages.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
区分“真实”输入和“模拟”输入的唯一方法(假设它是使用
keybd_event()
/mouse_event()
或SendInput() 生成的
)是通过SetWindowsHookEx()
使用低级键盘/鼠标钩子。WH_KEYBOARD_LL
和WH_MOUSE_LL
挂钩回调为模拟输入提供INJECTED
标志。The only way to distinguish between "real" input and "simulated" input (assuming it is being generated with
keybd_event()
/mouse_event()
orSendInput()
) is to use a low-level keyboard/mouse hook viaSetWindowsHookEx()
. TheWH_KEYBOARD_LL
andWH_MOUSE_LL
hook callbacks provideINJECTED
flags for simulated input.从 Windows 8 开始,有
GetCurrentInputMessageSource< /code>
函数。您可以使用它,并检查
originId
枚举中的以下值:Starting form Windows 8 there's the
GetCurrentInputMessageSource
function. You can use it, and check theoriginId
enum for the following value:我可能是错的,但屏幕键盘(以及其他模拟用户输入的应用程序)很可能使用 SendInput API:
因此,可能无法判断输入是否来自“真实”键盘。
I might be wrong, but the on-screen keyboard (and other applications that simulate user input) most probably uses the SendInput API:
So there is probably no way to tell whether the input is coming from a "real" keyboard or not.