如何在没有全局挂钩的情况下获得鼠标/键盘活动的通知?
我有一个浮动在最上面的透明窗口(WS_EX_TRANSPARENT)。
每当鼠标移动(屏幕上的任何位置)或键盘敲击时,它都需要显示相关信息(例如鼠标位置)。
是否可以在不使用全局挂钩的情况下捕获鼠标/键盘活动?防病毒软件几乎总是会因使用全局病毒而引发误报 钩子。
任何想法都非常感激。
I have a transparent window (WS_EX_TRANSPARENT) floating topmost.
Whenever there is a mouse move (anywhere on the screen) or keyboard stroke, it needs to display the related info (e.g. mouse position).
Is it possible to capture mouse/keyboard activities without using a global hook? Anti-virus software almost always triggers false alarms for the use of global
hooks.
Any idea greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我想, GetAsyncKeyState 和 GetCursorPos 可能会有所帮助。您可能可以让一个线程每 300-500 毫秒调用一次这些函数,并向您的主线程发布一条消息。
I guess, GetAsyncKeyState and GetCursorPos might help. You probably can have a thread calling these functions every 300-500 msec, and posting a message to your main thread.
您可以通过 RegisterRawInputDevices。看看这里,接受的答案中有一些链接RRUZ 的一个指向 C# 实现。这适用于窗口消息,不涉及钩子。
(使用此方法,您还可以获得有关输入来自的特定设备的信息,因此您可以区分多个键盘。这就是大多数以“use RegisterRawInputDevices”作为答案的问题的标题。但您也可以使用它来捕获输入,不关心来源。)
You could register for receiving raw input messages via RegisterRawInputDevices. Have a look over here, there are some links in the accepted answer of RRUZ, one points to a C# implementation. This works with window messages, no hooks involved.
(With this method you also get information about the specific device the input came from, so you could distinguish between multiple keyboards. That's where most questions having "use RegisterRawInputDevices" as answer are heading. But you can also use it to just capture the input, not caring about the source.)
您可以获得键盘/鼠标活动的通知 (
GetLastInputInfo
),并且我相当确定您可以获得光标位置 (GetMouseMovePointsEx
)。如果您不需要实际的键盘敲击,那么就可以了。如果你这样做,我认为这是不可能的......You can get notified of keyboard/mouse activity (
GetLastInputInfo
), and I am fairly certain you can get the cursor position (GetMouseMovePointsEx
). If you do not need the actual keyboard strokes, then that should do it. If you do, I do not think it can be done...LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();
UInt32 lastInputTick = lastInputInfo.dwTime;
return Environment.TickCount - (Int32)lastInputInfo.dwTime
此代码 (C#) 返回不活动时间(键盘和鼠标)。这样您就可以在用户处于非活动状态时拥有时间。
LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();
UInt32 lastInputTick = lastInputInfo.dwTime;
return Environment.TickCount - (Int32)lastInputInfo.dwTime
This code (C#) return the inactivity time (keyboard and mouse both). So you can have the time since the user is inactive.