当前位置: 代码迷 >> Windows Mobile >> Windows Mobile 讯息钩子(1)
  详细解决方案

Windows Mobile 讯息钩子(1)

热度:50   发布时间:2016-04-25 07:37:27.0
Windows Mobile 消息钩子(1)

    在Windows中,设置键盘钩子很多人都做过,但是在windows Mobile系统中并没有直接的函数支持。但是我们可以通过使用undocument api来实现。

一、定义参数

  1. #define WH_KEYBOARD_LL           20 
  2. #define HC_ACTION        0 
  3. typedef LRESULT(CALLBACK* HOOKPROC)(int code, WPARAM wParam, LPARAM lParam); 
  4. typedef HHOOK (WINAPI *_SetWindowsHookExW)(int, HOOKPROC, HINSTANCEDWORD); 
  5. typedef LRESULT (WINAPI *_CallNextHookEx)(HHOOKintWPARAMLPARAM); 
  6. typedef LRESULT (WINAPI *_UnhookWindowsHookEx)(HHOOK); 
  7. typedef struct 
  8.     DWORD vkCode; 
  9.     DWORD scanCode; 
  10.     DWORD flags; 
  11.     DWORD time; 
  12.     ULONG_PTR dwExtraInfo; 
  13. } KBDLLHOOKSTRUCT, *PKBDLLHOOKSTRUCT; 
  14.  
  15. static _SetWindowsHookExW       SetWindowsHookEx; 
  16. static _UnhookWindowsHookEx    UnhookWindowsHookEx; 
  17. static _CallNextHookEx                CallNextHookEx;   

二、实现代码

LLKeyboardHookCallbackFunction就是回调函数

  1. BOOL ActivateKBHook(HINSTANCE hInstance, 
  2.   HOOKPROC LLKeyboardHookCallbackFunction) 
  3. //we need to manually load these standard Win32 API calls 
  4. //MSDN states that these aren''t supported in WinCE 
  5.     SetWindowsHookEx        = NULL; 
  6.     CallNextHookEx            = NULL; 
  7.     UnhookWindowsHookEx        = NULL; 
  8. //从 coredll.dll 获取我们需要的函数 
  9.     g_hHookApiDLL = LoadLibrary(_T("coredll.dll")); 
  10.     if(g_hHookApiDLL == NULL) 
  11.    { 
  12.         return FALSE; 
  13.     } 
  14.     else 
  15.     { 
  16.         SetWindowsHookEx = (_SetWindowsHookExW)GetProcAddress(
  17. g_hHookApiDLL, _T("SetWindowsHookExW")); 
  18.         if(SetWindowsHookEx == NULL) 
  19.         { 
  20.             return FALSE; 
  21.         } 
  22.         else 
  23.         { 
  24.             g_hInstalledLLKBDhook = SetWindowsHookEx(WH_KEYBOARD_LL,
  25.  LLKeyboardHookCallbackFunction, hInstance, 0); 
  26.             if(g_hInstalledLLKBDhook == NULL) 
  27.             { 
  28.                 return FALSE; 
  29.             } 
  30.         } 
  31.         CallNextHookEx = (_CallNextHookEx)GetProcAddress(g_hHookApiDLL,
  32.  _T("CallNextHookEx")); 
  33.         if(CallNextHookEx == NULL) 
  34.         { 
  35.             return FALSE; 
  36.         } 
  37.         UnhookWindowsHookEx = (_UnhookWindowsHookEx)GetProcAddress(g_hHookApiDLL,
  38.  _T("UnhookWindowsHookEx")); 
  39.         if(UnhookWindowsHookEx == NULL) 
  40.         { 
  41.             return FALSE; 
  42.         } 
  43.     } 
  44.     return TRUE; 
  45.  
  46. BOOL DeactivateKBHook() 
  47.     //unload the hook 
  48.     if(g_hInstalledLLKBDhook != NULL) 
  49.     { 
  50.         UnhookWindowsHookEx(g_hInstalledLLKBDhook); 
  51.         g_hInstalledLLKBDhook = NULL; 
  52.     } 
  53.     //unload the coredll.dll 
  54.     if(g_hHookApiDLL != NULL) 
  55.     { 
  56.         FreeLibrary(g_hHookApiDLL); 
  57.         g_hHookApiDLL = NULL; 
  58.     } 
  59.     //we have terminated gracefully 
  60.     return TRUE; 

三、消息回调函数

一般的回调函数是这么写的,BlockKeyList是需要屏蔽的按键列表

  1. int CLoginDlg::BlockKeyList[] = {VK_TBACK,VK_TTALK,VK_TEND,VK_THOME}; 
  2. LRESULT CALLBACK CLoginDlg::LLKeyboardHookCallbackFunction(int nCode,
  3.  WPARAM wParam, LPARAM lParam) 
  4.     if(nCode >= HC_ACTION) 
  5.     { 
  6.         if(wParam == WM_KEYDOWN || wParam == WM_KEYUP
  7.  || wParam == WM_LBUTTONDOWN) 
  8.         {
  9.             int countKeyList = ARRAY_SIZEOF(BlockKeyList);
  10.             for(int i=0;i<countKeyList;i++) 
  11.             {
  12.                 if((((KBDLLHOOKSTRUCT*)lParam)->vkCode) == BlockKeyList[i])
  13.                 {
  14.                     return TRUE; 
  15.                 } 
  16.             } 
  17.         } 
  18.     } 
  19.     return CallNextHookEx(g_hInstalledLLKBDhook, nCode, wParam, lParam); 

   Windows Mobile下不使用DLL也可以实现全局键盘钩子)

   如果你只是要屏蔽所有按键的话可以简单的使用EnableHardwareKeyboard函数来实现。



  相关解决方案