我有一个在后台运行的应用程序(最小化/任务托盘).我需要能够检测鼠标活动(点击、移动)以及键盘活动.
I have an app that runs in the background (minimized/task tray). I need to be able to detect mouse activity (clicks, move) as well as keyboard activity.
考虑到我的窗口没有聚焦"的限制,最好的方法是什么?
What is the best way to do this given the constraint that my window is not "focused" ?
神奇的词是windows hooks.这些是通过对 <的 ap/invoke 调用创建的代码>SetWindowsHookEx.您可以设置一个挂钩来监视键盘和鼠标事件等.通常,此类挂钩对于应用程序来说是本地的,但您也可以创建全局挂钩.Microsoft 知识库 显示 方法.
The magic words are windows hooks. These are created with a p/invoke call to SetWindowsHookEx
. You can set up a hook to monitor, among others, keyboard and mouse events. Normally, such hooks are local to the application, but you can also create global hooks. The Microsoft KB shows how.
但是,请注意,并非所有类型的全局挂钩都可以在 .NET 中使用.特别是,您可以使用的只有两个:低级键盘和鼠标挂钩,称为 WH_KEYBOARD_LL
和 WH_MOUSE_LL代码>.幸运的是,这些正是您所需要的.
However, be aware that not all types of global hooks can be used from .NET. In particular, there are only two that you can use: the low-level keyboard and mouse hooks, known as WH_KEYBOARD_LL
and WH_MOUSE_LL
. Luckily, these are just what you need.
这篇关于捕获表单之外的鼠标/键盘事件(应用程序在后台运行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!