我希望编写一个小工具,当用户按下Windows按钮并向上或向下滚动鼠标滚轮时,该工具将捕获全局事件。当捕获到这样的事件时,我希望将所述输出重定向到Win- +或Win--(加/减)的虚拟击键组合。可以这样做吗?
如果保留了Windows键,则可以使用ctrl-alt等。
答案 0 :(得分:1)
由于它使用Windows密钥,因此可以使用热键绑定全局捕获密钥。在msdn注册RegisterHotKey。
编辑:似乎鼠标轮事件不像我假设的那样被视为键,并且无法为它们创建全局热键。
您必须创建一个全局窗口消息钩子并捕获WM_MOUSEWHEEL消息。但您可能必须在C / C ++中这样做。下面是完成此操作的C dll,您可以从C#调用Hook和Unhook来启用和禁用该功能。
警告:我尚未测试此代码,仅作为演示提供。
#include <windows.h>
HINSTANCE myInstance;
HHOOK thehook = 0;
BOOL isWinKeyDown = FALSE;
extern "C" LRESULT __declspec(dllexport)__stdcall CALLBACK HookHandler(int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode == WM_KEYDOWN && (wParam == VK_LWIN || wParam == VK_RWIN))
isWinKeyDown = TRUE;
else if (nCode == WM_KEYUP && (wParam == VK_LWIN || wParam == VK_RWIN))
isWinKeyDown = FALSE;
else if (nCode == WM_MOUSEHWHEEL && isWinKeyDown) {
if (HIWORD(wParam) > 0) { //mousewheel up
CallNextHookEx(thehook, WM_KEYDOWN, VK_ADD, 0);
CallNextHookEx(thehook, WM_KEYUP, VK_ADD, 0);
} else { //mousewheel down
CallNextHookEx(thehook, WM_KEYDOWN, VK_SUBTRACT, 0);
CallNextHookEx(thehook, WM_KEYUP, VK_SUBTRACT, 0);
}
return 0;
}
return CallNextHookEx(thehook, nCode, wParam, lParam);
}
BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD fwdReason, LPVOID lpvReserved) {
switch(fwdReason)
{
case DLL_PROCESS_ATTACH: {
DisableThreadLibraryCalls(hInstance);
myInstance = hInstance;
} break;
case DLL_THREAD_ATTACH:
break;
case DLL_PROCESS_DETACH:
break;
case DLL_THREAD_DETACH:
break;
}
return(TRUE); // The initialization was successful, a FALSE will abort
// the DLL attach
}
extern "C" void __declspec(dllexport) Hook() {
if (!thehook)
thehook = SetWindowsHookEx(WH_CALLWNDPROC, &HookHandler, myInstance, 0);
}
extern "C" void __declspec(dllexport) UnHook() {
if (thehook)
UnhookWindowsHookEx(thehook);
thehook = 0;
}
答案 1 :(得分:0)
绝对可以通过全局钩子完成,here是一个很好的CodeProject示例,说明如何执行此操作。