問題描述
我有一個在后臺運(yùn)行的應(yīng)用程序(最小化/任務(wù)托盤).我需要能夠檢測鼠標(biāo)活動(點(diǎn)擊、移動)以及鍵盤活動.
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 調(diào)用創(chuàng)建的代碼>SetWindowsHookEx.您可以設(shè)置一個掛鉤來監(jiān)視鍵盤和鼠標(biāo)事件等.通常,此類掛鉤對于應(yīng)用程序來說是本地的,但您也可以創(chuàng)建全局掛鉤.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 中使用.特別是,您可以使用的只有兩個:低級鍵盤和鼠標(biāo)掛鉤,稱為 WH_KEYBOARD_LL
和 WH_MOUSE_LL代碼>.幸運(yùn)的是,這些正是您所需要的.
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.
這篇關(guān)于捕獲表單之外的鼠標(biāo)/鍵盤事件(應(yīng)用程序在后臺運(yùn)行)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!