不用在dll中,挂钩全局键盘钩子
发布时间:2018年1月27日 作者:未知 查看次数:1039
使用WH_KEYBOARD_LL,不要使用WH_KEYBOARD,则可以把全局键盘钩子过程放在exe中,不需要dll。 例代码 : VC6 新建Win32 Application工程,选A simple Win32 application,修改代码如下: =================================================== #include "stdafx.h" #define ULONG_PTR ULONG #define WH_KEYBOARD_LL 13 typedef struct { DWORD vkCode; DWORD scanCode; DWORD flags; DWORD time; ULONG_PTR dwExtraInfo; } KBDLLHOOKSTRUCT, *PKBDLLHOOKSTRUCT; HHOOK keyboardHook = 0; LRESULT CALLBACK HookCallback(int code, WPARAM wParam, LPARAM lParam); // Foward declarations of functions included in this code module: int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG msg; MessageBox(0,"挂载全局键盘钩子","aa",0); keyboardHook = SetWindowsHookExA(WH_KEYBOARD_LL, HookCallback, GetModuleHandleA(0), 0); if (keyboardHook == 0) { MessageBox(0,"挂载全局键盘钩子失败","aa",0); return -1; } while (GetMessage (&msg, NULL, 0, 0)) //试着用while(TRUE)而不用消息循环不起作用 { TranslateMessage (&msg); DispatchMessage (&msg); } MessageBox(0,"程序正常退出,卸载全局键盘钩子","aa",0); UnhookWindowsHookEx(keyboardHook); return msg.wParam; } LRESULT CALLBACK HookCallback(int code, WPARAM wParam, LPARAM lParam) { KBDLLHOOKSTRUCT *ks = (KBDLLHOOKSTRUCT*)lParam; if(ks->vkCode == 'Q' && wParam==WM_KEYDOWN) //按下Q键 { PostQuitMessage(0); return 1; } return CallNextHookEx(0, code, wParam, lParam); //CallNextHookEx 的第一个参数忽略? // return CallNextHookEx(keyboardHook, code, wParam, lParam); } //如果要检测 a-z 键,要使用大写 A-Z 不然会无效并且检测到其他按键,小写的 a-z 与其他VK_键重叠(baidu)
|
|
|