Windows Mobile Message hook (1)

Source: Internet
Author: User

Many people have done keyboard hook setting in windows, but there is no direct Function Support in Windows Mobile System. However, we can use the undocument API.

1. Define parameters
 
 
  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, HINSTANCE, DWORD); 
  5. typedef LRESULT (WINAPI *_CallNextHookEx)(HHOOK, int, WPARAM, LPARAM); 
  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;   
II. Implementation Code

Llkeyboardhookcallbackfunction is the callback function.

 
 
  1. Bool activatekbhook (hinstance,
  2. Hookproc llkeyboardhookcallbackfunction)
  3. {
  4. // We need to manually load these standard Win32 API CILS
  5. // Msdn states that these aren'' t supported in WinCE
  6. Setwindowshookex = NULL;
  7. Callnexthookex = NULL;
  8. Unhookwindowshookex = NULL;
  9. // Obtain the required function from coredll. dll.
  10. G_hhookapidll = loadlibrary (_ T ("coredll. dll "));
  11. If (g_hhookapidll = NULL)
  12. {
  13. Return false;
  14. }
  15. Else
  16. {
  17. Setwindowshookex = (_ setwindowshookexw) getprocaddress (
  18. G_hhookapidll, _ T ("setwindowshookexw "));
  19. If (setwindowshookex = NULL)
  20. {
  21. Return false;
  22. }
  23. Else
  24. {
  25. G_hinstalledllkbdhook = setwindowshookex (wh_keyboard_ll,
  26. Llkeyboardhookcallbackfunction, hinstance, 0 );
  27. If (g_hinstalledllkbdhook = NULL)
  28. {
  29. Return false;
  30. }
  31. }
  32. Callnexthookex = (_ callnexthookex) getprocaddress (g_hhookapidll,
  33. _ T ("callnexthookex "));
  34. If (callnexthookex = NULL)
  35. {
  36. Return false;
  37. }
  38. Unhookwindowshookex = (_ unhookwindowshookex) getprocaddress (g_hhookapidll,
  39. _ T ("unhookwindowshookex "));
  40. If (unhookwindowshookex = NULL)
  41. {
  42. Return false;
  43. }
  44. }
  45. Return true;
  46. }
  47.  
  48. Bool deactivatekbhook ()
  49. {
  50. // Unload the hook
  51. If (g_hinstalledllkbdhook! = NULL)
  52. {
  53. Unhookwindowshookex (g_hinstalledllkbdhook );
  54. G_hinstalledllkbdhook = NULL;
  55. }
  56. // Unload the coredll. dll
  57. If (g_hhookapidll! = NULL)
  58. {
  59. Freelibrary (g_hhookapidll );
  60. G_hhookapidll = NULL;
  61. }
  62. // We have terminated gracefully
  63. Return true;
  64. }
Iii. Message callback function

This is the general callback function, and blockkeylist is the list of buttons to be blocked.

 
 
  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); 

Global keyboard hooks can be implemented without using DLL in Windows Mobile)

If you only want to block all the buttons, you can simply use the enablehardwarekeyboard function.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.