捕获表单之外的鼠标/键盘事件(应用程序在后台运行)

发布于 2024-09-10 16:29:33 字数 92 浏览 1 评论 0原文

我有一个在后台运行的应用程序(最小化/任务托盘)。我需要能够检测鼠标活动(点击、移动)以及键盘活动。

考虑到我的窗口没有“聚焦”的限制,最好的方法是什么?

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" ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谢绝鈎搭 2024-09-17 16:29:33

看看这个库 globalmousekeyhook
它是 100% 托管的 C# 代码,用于安装全局鼠标和键盘挂钩。
它将低级挂钩包装到常见的窗口形式键盘和鼠标事件中。

Take a look at this library globalmousekeyhook.
It is 100% managed c# code to install global mouse and keyboard hooks.
It wraps low level hooks into common windows forms keyboard and mouse events.

幻梦 2024-09-17 16:29:33

神奇的词是windows hooks。这些是通过 ap/invoke 调用 <代码>SetWindowsHookEx。您可以设置一个挂钩来监视键盘和鼠标事件等。通常,此类挂钩是应用程序本地的,但您也可以创建全局挂钩。 Microsoft 知识库展示了如何操作。

但是,请注意,并非所有类型的全局挂钩都可以在 .NET 中使用。特别是,您可以使用两个:低级键盘和鼠标挂钩,称为WH_KEYBOARD_LLWH_MOUSE_LL。幸运的是,这些正是您所需要的。

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文