C #: Comment capturer la souris mondiale / événements HID
Question
Je veux écrire un petit outil qui permettra de saisir un événement mondial lorsque l'utilisateur appuie sur le bouton Windows et fait défiler la molette vers le haut ou vers le bas. Quand un tel événement est capturé, je souhaite rediriger ladite sortie à une combinaison de touches virtuel de Win- + ou Win-- (plus / moins). Cela peut-il être fait?
Si la touche Windows est réservé, ctrl-alt ou tel serait le faire.
La solution
Comme il utilise la clé Windows, la clé peut être capturée à l'échelle mondiale en utilisant un raccourci clavier contraignant. RegisterHotKey msdn.
Edit: Il semble que les événements mouseWheel ne sont pas traités comme des clés que je supposais et il n'y a aucun moyen de faire un raccourci clavier global pour les
.Vous devrez faire un crochet global de message de fenêtre et de piéger le message WM_MOUSEWHEEL. Mais vous pourriez avoir à faire en C / C ++. Une dll C pour y arriver est ci-dessous, vous pouvez appeler Crochet et décrochez-C # pour activer et désactiver la fonction.
AVERTISSEMENT:. Je n'ai pas testé ce code et est prévu comme une démonstration seulement
#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;
}