CE 6 下键盘钩子(HOOK)的实现
扫描二维码
随时随地手机看文章
// 已经写过一个 CE5.0 下的键盘钩子:http://blog.csdn.net/91program/article/details/1961570 // 前一段时间在坛子看到有人在问 CE6.0 下钩子的实现,有的说不行,有的说行,最后也没有结果 // 今天试了下,是可以的,包括鼠标的钩子。不过速度上好像是有些问题,待有时间再看看吧。 // 看代码吧。 // KeyBoardHook.h #ifdef KEYBOARDHOOK_EXPORTS #define KEYBOARDHOOK_API __declspec(dllexport) #else #define KEYBOARDHOOK_API __declspec(dllimport) #endif // This class is exported from the KeyBoardHook.dll class KEYBOARDHOOK_API CKeyBoardHook { public: CKeyBoardHook(void); // TODO: add your methods here. }; extern "C" KEYBOARDHOOK_API void InstallKeyBoardHook(void); extern "C" KEYBOARDHOOK_API void UnKeyBoardHook(void); extern "C" KEYBOARDHOOK_API LRESULT CALLBACK KeyBoardProc(int nCode, WPARAM wParam, LPARAM lParam); // KeyBoardHook.cpp // KeyBoardHook.cpp : Defines the entry point for the DLL application. // #include "stdafx.h" #include "KeyBoardHook.h" #include// 告诉编译器将变量放入它自己的数据共享节中 #pragma data_seg("KeyHookData") HINSTANCE ghInst = NULL; HHOOK ghKeyHook = NULL; #pragma data_seg() // 告诉编译器设置共享节的访问方式为:读,写,共享 #pragma comment(linker, "/SECTION:KeyHookData,RWS") BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: ghInst = (HINSTANCE)hModule; break; case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; } return TRUE; } // This is the constructor of a class that has been exported. // see KeyBoardHook.h for the class definition CKeyBoardHook::CKeyBoardHook() { return; } extern "C" KEYBOARDHOOK_API void InstallKeyBoardHook(void) { RETAILMSG(1,(L"[Keyboard HOOK]Install HOOK: 0x%xrn",ghInst)); if(ghInst) { ghKeyHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyBoardProc, ghInst, 0); if(NULL == ghKeyHook) { RETAILMSG(1,(L"[Keyboard HOOK]HOOK create failed: %drn",GetLastError())); } } } extern "C" KEYBOARDHOOK_API void UnKeyBoardHook(void) { RETAILMSG(1,(L"[Keyboard HOOK]Uninstall HOOK:0x%xrn",ghKeyHook)); if(ghKeyHook) { UnhookWindowsHookEx(ghKeyHook); ghKeyHook = NULL; } ghInst = NULL; } // CE6 的键盘 HOOK 的响应速度慢, 原因不明! // CallNextHookEx(hKeyHook, nCode, wParam, lParam); //继续传递消息 // SIP 上仅 Ctl/Shift/CAP/Esc/Backspace 可以处理到 // key value 17/ 16 / 20/ 27/ 8 extern "C" KEYBOARDHOOK_API LRESULT CALLBACK KeyBoardProc(int nCode, WPARAM wParam, LPARAM lParam) { #if _DEBUG RETAILMSG(1,(L"[Keyboard HOOK]Proc,Code is: %d,Down/Up is: %srn",nCode,256 == wParam ? L"Down" : L"Up")); #endif if(HC_ACTION != nCode) { return CallNextHookEx(ghKeyHook, nCode, wParam, lParam); } PKBDLLHOOKSTRUCT pKBHookStruct = (PKBDLLHOOKSTRUCT)lParam; RETAILMSG(1,(L"[Keyboard HOOK]vkCode value is: %drn",pKBHookStruct->vkCode)); RETAILMSG(1,(L"[Keyboard HOOK]scanCode value is: %drn",pKBHookStruct->scanCode)); RETAILMSG(1,(L"[Keyboard HOOK]dwExtraInfo value is: %drn",pKBHookStruct->dwExtraInfo)); if(WM_KEYDOWN == wParam) { // 响应按键声,并限制需要向上 Post 的 vkCode switch(pKBHookStruct->vkCode) { case VK_UP: case VK_DOWN: case VK_LEFT: case VK_RIGHT: break; default: break; } } else if (WM_KEYUP == wParam) { // 限制需要向上Post的vkCode switch (pKBHookStruct->vkCode) { case VK_UP: case VK_DOWN: case VK_LEFT: case VK_RIGHT: break; default: break; } } // 若注释此句, 则被 HOOK 到的按键不再发到 CE 系统 return CallNextHookEx(ghKeyHook, nCode, wParam, lParam); // 继续传递消息 } // 调用示例 // Keyboard hook test typedef void (*pInstallKeyboardHook)(void); typedef void (*pUnKeyboardHook)(void); HINSTANCE ghKeyboardModule = NULL; pInstallKeyboardHook InKeyBoardHook = NULL; pUnKeyboardHook UnKeyBoardHook = NULL; ghKeyboardModule = LoadLibrary(L"\AppStore\SystemInfo\KeyBoardHook.dll"); if(ghKeyboardModule) { InKeyBoardHook = (pInstallKeyboardHook)GetProcAddress(ghKeyboardModule, L"InstallKeyBoardHook"); UnKeyBoardHook = (pUnKeyboardHook)GetProcAddress(ghKeyboardModule, L"UnKeyBoardHook"); } if(NULL != InKeyBoardHook) { InKeyBoardHook(); }