#include <stdio.h>
#include <windows.h>
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
printf("Keyboard event, nCode = %d, wParam = %d, lParam = 0x%.8X\n", nCode, wParam, lParam);
return (LRESULT)NULL;
}
void main() {
HHOOK HookHandle = SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC)KeyboardProc, (HINSTANCE) NULL, GetCurrentThreadId());
printf("Hook handle = 0x%.8X\n", HookHandle);
MSG message;
while (GetMessage(&message,NULL,0,0)) {
TranslateMessage(&message);
DispatchMessage(&message);
}
UnhookWindowsHookEx(HookHandle);
}
預計輸出類似「鍵盤事件...」,但它不起作用,無論我輸入什麼,它總是保持沉默。爲什麼SetWindowsHookEx不能在CLI中使用(在main()中代碼而不是dllmain等)?
的編譯選項很簡單:gcc的-o test.exe的test.c的
因此,無法在沒有DLL的控制檯程序中捕獲鍵盤事件? – Chen 2011-03-13 17:18:59
「捕獲」不是很具體。你可以設置一個WH_KEYBOARD_LL鉤子,它不需要DLL。 – 2011-03-13 19:18:05
WH_KEYBOARD_LL是一個全局鉤子,我認爲它必須在DLL中,因爲這個鏈接說:[link](http://www.codeguru.com/cpp/wp/system/keyboard/article.php/c5699) – Chen 2011-03-14 10:27:24