捕获表单之外的鼠标/键盘事件(应用程序在后台运行)
- 作者: 你过来--我保证打死你
- 来源: 51数据库
- 2023-02-08
问题描述
我有一个在后台运行的应用程序(最小化/任务托盘).我需要能够检测鼠标活动(点击、移动)以及键盘活动.
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.
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源