MFC学习 消息钩子使用
HANDLE h_KeyBoard; //当前进程的钩子, 另外WH_KEYBOARD_LL, 与WH_MOUSE_LL参数时, 是获取的底层的消息, 相当于获取的全局的 g_hKeyBoard = SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, NULL, GetCurrentThreadId()); //全局钩子, 全局钩子要填写dll的句柄, 如用WH_GETMESSAGE可以获取其它进程的消息. h_KeyBoard =SetWindowsHookEx(WH_MOUSE, MouseProc, GetModuleHandle("HookGlobal"), NULL); //这儿是dll的名字, 全局钩子要dll名字, 其实GetMoudleHandle里面可以填写NULL UnhookWindowsHookEx(g_hKeyBoard);
LRESULT CALLBACK KeyboardProc( int code, // hook code WPARAM wParam, // virtual-key code LPARAM lParam // keystroke-message information ) { /* if(VK_SPACE == wParam || ((lParam >> 29 & 1) && VK_F4 == wParam)) return 1; else return CallNextHookEx(g_hKeyBoard, code, wParam, lParam); */ if(VK_F2 == wParam) { ::SendMessage((HWND)g_hWnd, WM_CLOSE, 0, 0); UnhookWindowsHookEx(g_hKeyBoard); UnhookWindowsHookEx(g_hMouse); } return 1; }