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.
这篇关于捕获表单之外的鼠标/键盘事件(应用程序在后台运行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:捕获表单之外的鼠标/键盘事件(应用程序在后台运
基础教程推荐
- MS Visual Studio .NET 的替代品 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01