.h class CFullWindowDlg : public CDialogEx { ... void SetHook(); } .cpp ///全局变量 HWND g_hWnd = NULL; //窗口句柄 HHOOK g_hlowKeyHook = NULL; //低级键盘钩子句柄 LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam); /// //安装钩子 void CFullWindowDlg::SetHook() //为了操作窗体,我们将主窗体的句柄传进去 { g_hWnd = this->m_hWnd; g_hlowKeyHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, AfxGetApp()->m_hInstance, NULL); } //低级键盘钩子 LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) { bool isClick = false; PKBDLLHOOKSTRUCT LowKey = NULL; //该结构体包含底层键盘输入事件的信息 if (nCode == HC_ACTION) //HC_ACTION 表示有消息 { LowKey = (PKBDLLHOOKSTRUCT)lParam; switch (wParam) { case WM_KEYDOWN: { isClick = 1; if (LowKey->vkCode == VK_F8) // 后门 { // ::MessageBox(NULL,"触发钩子后门程序,正在卸载钩子~~关闭程序","提示",MB_OK); SendMessage(g_hWnd, WM_CLOSE, 0, 0); //发送关闭主窗体消息 UnhookWindowsHookEx(g_hlowKeyHook); //卸载低级键盘钩子 } break; } case WM_SYSKEYDOWN: case WM_KEYUP: case WM_SYSKEYUP: { // 屏蔽Win isClick = (LowKey->vkCode == VK_LWIN) || (LowKey->vkCode == VK_RWIN) || //屏蔽Alt+F4 ((LowKey->vkCode == VK_F4) && ((LowKey->flags & LLKHF_ALTDOWN) != 0)) || // 屏蔽Alt+Tab ((LowKey->vkCode == VK_TAB) && ((LowKey->flags & LLKHF_ALTDOWN) != 0)) || // 屏蔽Alt+Esc ((LowKey->vkCode == VK_ESCAPE) && ((LowKey->flags & LLKHF_ALTDOWN) != 0)) || // 屏蔽Ctrl+Esc ((LowKey->vkCode == VK_ESCAPE) && ((GetKeyState(VK_CONTROL) & 0x8000) != 0)); break; } default: break; } } if (isClick) { return 1; } return CallNextHookEx(g_hlowKeyHook, nCode, wParam, lParam); //传给下一个钩子 }