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
- #define WH_KEYBOARD_LL 20
- #define HC_ACTION 0
- typedef LRESULT(CALLBACK* HOOKPROC)(int code, WPARAM wParam, LPARAM lParam);
- typedef HHOOK (WINAPI *_SetWindowsHookExW)(int, HOOKPROC, HINSTANCE, DWORD);
- typedef LRESULT (WINAPI *_CallNextHookEx)(HHOOK, int, WPARAM, LPARAM);
- typedef LRESULT (WINAPI *_UnhookWindowsHookEx)(HHOOK);
- typedef struct
- {
- DWORD vkCode;
- DWORD scanCode;
- DWORD flags;
- DWORD time;
- ULONG_PTR dwExtraInfo;
- } KBDLLHOOKSTRUCT, *PKBDLLHOOKSTRUCT;
-
- static _SetWindowsHookExW SetWindowsHookEx;
- static _UnhookWindowsHookEx UnhookWindowsHookEx;
- static _CallNextHookEx CallNextHookEx;
II. Implementation Code
Llkeyboardhookcallbackfunction is the callback function.
- Bool activatekbhook (hinstance,
- Hookproc llkeyboardhookcallbackfunction)
- {
- // We need to manually load these standard Win32 API CILS
- // Msdn states that these aren'' t supported in WinCE
- Setwindowshookex = NULL;
- Callnexthookex = NULL;
- Unhookwindowshookex = NULL;
- // Obtain the required function from coredll. dll.
- G_hhookapidll = loadlibrary (_ T ("coredll. dll "));
- If (g_hhookapidll = NULL)
- {
- Return false;
- }
- Else
- {
- Setwindowshookex = (_ setwindowshookexw) getprocaddress (
- G_hhookapidll, _ T ("setwindowshookexw "));
- If (setwindowshookex = NULL)
- {
- Return false;
- }
- Else
- {
- G_hinstalledllkbdhook = setwindowshookex (wh_keyboard_ll,
- Llkeyboardhookcallbackfunction, hinstance, 0 );
- If (g_hinstalledllkbdhook = NULL)
- {
- Return false;
- }
- }
- Callnexthookex = (_ callnexthookex) getprocaddress (g_hhookapidll,
- _ T ("callnexthookex "));
- If (callnexthookex = NULL)
- {
- Return false;
- }
- Unhookwindowshookex = (_ unhookwindowshookex) getprocaddress (g_hhookapidll,
- _ T ("unhookwindowshookex "));
- If (unhookwindowshookex = NULL)
- {
- Return false;
- }
- }
- Return true;
- }
-
- Bool deactivatekbhook ()
- {
- // Unload the hook
- If (g_hinstalledllkbdhook! = NULL)
- {
- Unhookwindowshookex (g_hinstalledllkbdhook );
- G_hinstalledllkbdhook = NULL;
- }
- // Unload the coredll. dll
- If (g_hhookapidll! = NULL)
- {
- Freelibrary (g_hhookapidll );
- G_hhookapidll = NULL;
- }
- // We have terminated gracefully
- Return true;
- }
Iii. Message callback function
This is the general callback function, and blockkeylist is the list of buttons to be blocked.
- int CLoginDlg::BlockKeyList[] = {VK_TBACK,VK_TTALK,VK_TEND,VK_THOME};
- LRESULT CALLBACK CLoginDlg::LLKeyboardHookCallbackFunction(int nCode,
- WPARAM wParam, LPARAM lParam)
- {
- if(nCode >= HC_ACTION)
- {
- if(wParam == WM_KEYDOWN || wParam == WM_KEYUP
- || wParam == WM_LBUTTONDOWN)
- {
- int countKeyList = ARRAY_SIZEOF(BlockKeyList);
- for(int i=0;i<countKeyList;i++)
- {
- if((((KBDLLHOOKSTRUCT*)lParam)->vkCode) == BlockKeyList[i])
- {
- return TRUE;
- }
- }
- }
- }
- 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.