参考链接:Windows控制台下响应键盘消息
#include <windows.h>
#include <stdio.h>
HHOOK g_Hook;
LRESULT CALLBACK KeyboardProc(int code, WPARAM w, LPARAM l)
{
if (w == WM_KEYDOWN)
printf("摁下按键\n");
else if (w == WM_KEYUP)
printf("抬起按键\n");
return CallNextHookEx(g_Hook, code, w, l);
}
int main()
{
g_Hook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardProc, GetModuleHandle(NULL), 0);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
UnhookWindowsHookEx(g_Hook);
return 0;
}