// HookapiTest.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <Windows.h> #include <iostream> using namespace std; HHOOK keyboardHook = 0; LRESULT CALLBACK HookCallback(int code, WPARAM wParam, LPARAM lParam) { KBDLLHOOKSTRUCT *ks = (KBDLLHOOKSTRUCT*)lParam; if(ks->vkCode == 'A') { cout << "A键拦截了!" << endl; return 1; } return CallNextHookEx(0, code, wParam, lParam); } int main() { keyboardHook = SetWindowsHookExA(WH_KEYBOARD_LL, HookCallback, GetModuleHandleA(0), 0); if (keyboardHook == 0) { cout << "键盘钩子挂失败了!" << endl; return -1; } cout << "钩子挂好了!" << endl; MSG msg; while(1) { if (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessageW(&msg); } else Sleep(0); //避免CPU全负载运行 } UnhookWindowsHookEx(keyboardHook); cout << "正常结束!" << endl; return 0; }