捕捉形式之外的鼠标/键盘事件(在后台程序运行) [英] Capturing mouse/keyboard events outside of form (app running in background)

查看:374
本文介绍了捕捉形式之外的鼠标/键盘事件(在后台程序运行)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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钩子。这些具有p创建/ Invoke调用到<一个href=\"http://msdn.microsoft.com/en-us/library/ms644990%28VS.85%29.aspx\"><$c$c>SetWindowsHookEx.您可以设置一个钩子监视等等,键盘和鼠标事件。通常情况下,这种挂钩是本地应用程序,但你也可以创建全局钩子。微软KB 显示如何。

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.

这篇关于捕捉形式之外的鼠标/键盘事件(在后台程序运行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆