Detecting simulated keyboard/mouse input

谁都会走 提交于 2019-12-18 07:18:57

问题


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.


回答1:


I might be wrong, but the on-screen keyboard (and other applications that simulate user input) most probably uses the SendInput API:

SendInput operates at the bottom level of the input stack. It is just a backdoor into the same input mechanism that the keyboard and mouse drivers use to tell the window manager that the user has generated input. Source: http://blogs.msdn.com/b/oldnewthing/archive/2010/12/21/10107494.aspx

So there is probably no way to tell whether the input is coming from a "real" keyboard or not.




回答2:


The only way to distinguish between "real" input and "simulated" input (assuming it is being generated with keybd_event()/mouse_event() or SendInput()) is to use a low-level keyboard/mouse hook via SetWindowsHookEx(). The WH_KEYBOARD_LL and WH_MOUSE_LL hook callbacks provide INJECTED flags for simulated input.




回答3:


Starting form Windows 8 there's the GetCurrentInputMessageSource function. You can use it, and check the originId enum for the following value:

IMO_INJECTED - The input message has been injected (through the SendInput function) by an application that doesn't have the UIAccess attribute set to TRUE in its manifest file.



来源:https://stackoverflow.com/questions/4553777/detecting-simulated-keyboard-mouse-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!