Capturing mouse/keyboard events outside of form (app running in background)(捕获表单之外的鼠标/键盘事件(应用程序在后台运行))
问题描述
我有一个在后台运行的应用程序(最小化/任务托盘).我需要能够检测鼠标活动(点击、移动)以及键盘活动.
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.
这篇关于捕获表单之外的鼠标/键盘事件(应用程序在后台运行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:捕获表单之外的鼠标/键盘事件(应用程序在后台运


- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01