Windows控制台下响应键盘消息

利用hook技术,hook键盘消息,每次摁下和松开按键时都打印信息

源代码如下

#include 
#include 
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()
{
	MSG msg;
	g_Hook = SetWindowsHookEx(WH_KEYBOARD_LL,KeyboardProc,GetModuleHandle(NULL),0);
	while(GetMessage(&msg,NULL,0,0))
    {
       TranslateMessage(&msg);
       DispatchMessage(&msg);
    }
    UnhookWindowsHookEx(g_Hook);
	return 0;
}


你可能感兴趣的:(Windows控制台下响应键盘消息)