• WinCE 6 下键盘钩子(HOOK)


     1 // 已经写过一个 CE5.0 下的键盘钩子:http://blog.csdn.net/91program/article/details/1961570  
     2 // 前一段时间在坛子看到有人在问 CE6.0 下钩子的实现,有的说不行,有的说行,最后也没有结果  
     3 // 今天试了下,是可以的,包括鼠标的钩子。不过速度上好像是有些问题,待有时间再看看吧。   
     4 // 看代码吧。  
     5   
     6 // KeyBoardHook.h   
     7   
     8 #ifdef KEYBOARDHOOK_EXPORTS  
     9 #define KEYBOARDHOOK_API __declspec(dllexport)  
    10 #else  
    11 #define KEYBOARDHOOK_API __declspec(dllimport)  
    12 #endif  
    13   
    14 // This class is exported from the KeyBoardHook.dll  
    15 class KEYBOARDHOOK_API CKeyBoardHook  
    16 {  
    17 public:  
    18   CKeyBoardHook(void);  
    19   // TODO: add your methods here.  
    20 };  
    21   
    22 extern "C" KEYBOARDHOOK_API void InstallKeyBoardHook(void);  
    23 extern "C" KEYBOARDHOOK_API void UnKeyBoardHook(void);  
    24 extern "C" KEYBOARDHOOK_API LRESULT CALLBACK KeyBoardProc(int nCode, WPARAM wParam, LPARAM lParam);  
      1 // KeyBoardHook.cpp   
      2 // KeyBoardHook.cpp : Defines the entry point for the DLL application.  
      3 //  
      4   
      5 #include "stdafx.h"  
      6 #include "KeyBoardHook.h"  
      7 #include <Pwinuser.h>  
      8   
      9 // 告诉编译器将变量放入它自己的数据共享节中  
     10 #pragma data_seg("KeyHookData")  
     11   
     12 HINSTANCE ghInst = NULL;  
     13 HHOOK ghKeyHook = NULL;  
     14   
     15 #pragma data_seg()  
     16   
     17 // 告诉编译器设置共享节的访问方式为:读,写,共享  
     18 #pragma comment(linker, "/SECTION:KeyHookData,RWS")  
     19   
     20 BOOL APIENTRY DllMain(HANDLE hModule,DWORD  ul_reason_for_call,LPVOID lpReserved)  
     21 {  
     22   switch (ul_reason_for_call)  
     23   {  
     24   case DLL_PROCESS_ATTACH:  
     25     ghInst = (HINSTANCE)hModule;  
     26     break;  
     27   case DLL_THREAD_ATTACH:  
     28   case DLL_THREAD_DETACH:  
     29   case DLL_PROCESS_DETACH:  
     30     break;  
     31   }  
     32   return TRUE;  
     33 }  
     34   
     35 // This is the constructor of a class that has been exported.  
     36 // see KeyBoardHook.h for the class definition  
     37 CKeyBoardHook::CKeyBoardHook()  
     38 {   
     39   return;   
     40 }  
     41   
     42 extern "C" KEYBOARDHOOK_API void InstallKeyBoardHook(void)  
     43 {  
     44   RETAILMSG(1,(L"[Keyboard HOOK]Install HOOK: 0x%x
    ",ghInst));  
     45   if(ghInst)  
     46   {  
     47     ghKeyHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyBoardProc, ghInst, 0);  
     48     if(NULL == ghKeyHook)  
     49     {  
     50       RETAILMSG(1,(L"[Keyboard HOOK]HOOK create failed: %d
    ",GetLastError()));  
     51     }  
     52   }  
     53 }  
     54   
     55 extern "C" KEYBOARDHOOK_API void UnKeyBoardHook(void)  
     56 {  
     57   RETAILMSG(1,(L"[Keyboard HOOK]Uninstall HOOK:0x%x
    ",ghKeyHook));  
     58   if(ghKeyHook)  
     59   {  
     60     UnhookWindowsHookEx(ghKeyHook);  
     61     ghKeyHook = NULL;  
     62   }  
     63   
     64   ghInst = NULL;  
     65 }  
     66   
     67 // CE6 的键盘 HOOK 的响应速度慢, 原因不明!  
     68 // CallNextHookEx(hKeyHook, nCode, wParam, lParam); //继续传递消息  
     69 // SIP 上仅 Ctl/Shift/CAP/Esc/Backspace 可以处理到  
     70 // key value 17/ 16  / 20/ 27/ 8  
     71 extern "C" KEYBOARDHOOK_API LRESULT CALLBACK KeyBoardProc(int nCode, WPARAM wParam, LPARAM lParam)  
     72 {  
     73 #if _DEBUG  
     74   RETAILMSG(1,(L"[Keyboard HOOK]Proc,Code is: %d,Down/Up is: %s
    ",nCode,256 == wParam ? L"Down" : L"Up"));  
     75 #endif  
     76   
     77   if(HC_ACTION != nCode)  
     78   {  
     79     return CallNextHookEx(ghKeyHook, nCode, wParam, lParam);  
     80   }  
     81   
     82   PKBDLLHOOKSTRUCT pKBHookStruct = (PKBDLLHOOKSTRUCT)lParam;  
     83   RETAILMSG(1,(L"[Keyboard HOOK]vkCode value is: %d
    ",pKBHookStruct->vkCode));  
     84   RETAILMSG(1,(L"[Keyboard HOOK]scanCode value is: %d
    ",pKBHookStruct->scanCode));  
     85   RETAILMSG(1,(L"[Keyboard HOOK]dwExtraInfo value is: %d
    ",pKBHookStruct->dwExtraInfo));  
     86   
     87   if(WM_KEYDOWN == wParam)  
     88   {  
     89     // 响应按键声,并限制需要向上 Post 的 vkCode  
     90     switch(pKBHookStruct->vkCode)  
     91     {  
     92     case VK_UP:  
     93     case VK_DOWN:  
     94     case VK_LEFT:  
     95     case VK_RIGHT:  
     96       break;  
     97     default:  
     98       break;  
     99     }  
    100   }  
    101   else if (WM_KEYUP == wParam)  
    102   {  
    103     // 限制需要向上Post的vkCode  
    104     switch (pKBHookStruct->vkCode)  
    105     {  
    106     case VK_UP:  
    107     case VK_DOWN:  
    108     case VK_LEFT:  
    109     case VK_RIGHT:  
    110       break;  
    111     default:  
    112       break;  
    113     }  
    114   }  
    115   
    116   // 若注释此句, 则被 HOOK 到的按键不再发到 CE 系统   
    117   return CallNextHookEx(ghKeyHook, nCode, wParam, lParam);  // 继续传递消息  
    118 }  
    119   
    120   
    121 // 调用示例  
    122   
    123 // Keyboard hook test  
    124 typedef void (*pInstallKeyboardHook)(void);  
    125 typedef void (*pUnKeyboardHook)(void);  
    126 HINSTANCE ghKeyboardModule = NULL;  
    127 pInstallKeyboardHook  InKeyBoardHook = NULL;  
    128 pUnKeyboardHook  UnKeyBoardHook = NULL;  
    129   
    130   
    131 ghKeyboardModule = LoadLibrary(L"\AppStore\SystemInfo\KeyBoardHook.dll");  
    132 if(ghKeyboardModule)  
    133 {  
    134   InKeyBoardHook = (pInstallKeyboardHook)GetProcAddress(ghKeyboardModule, L"InstallKeyBoardHook");  
    135   UnKeyBoardHook = (pUnKeyboardHook)GetProcAddress(ghKeyboardModule, L"UnKeyBoardHook");  
    136 }  
    137 if(NULL != InKeyBoardHook)  
    138 {  
    139   InKeyBoardHook();  
    140 }  
  • 相关阅读:
    Java入门:基础算法之从字符串中找到重复的字符
    Java入门:基础算法之产生随机数
    Java入门:基础算法之线性搜索
    Java入门:基础算法之检查奇偶性
    安装hadoop1.2.1集群环境
    Linux上安装JDK
    改变HTML中超链接的显示样式
    【Nutch2.2.1源代码分析之5】索引的基本流程
    【Nutch2.2.1源代码分析之4】Nutch加载配置文件的方法
    java生成UUID通用唯一识别码 (Universally Unique Identifier)
  • 原文地址:https://www.cnblogs.com/91program/p/5206465.html
Copyright © 2020-2023  润新知