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

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_LLWH_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.

这篇关于捕获表单之外的鼠标/键盘事件(应用程序在后台运行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)