Windows Mobile keyboard hook practices

Source: Internet
Author: User

When you need to use the soft1 and soft2 buttons, you also tried a variety of solutions and finally selected a hook function. However, some problems still occurred during use, some solutions have not been implemented yet. There are many implementations on the Internet below. Check this

Wincekbhook. h

# Ifndef _ wince_kb_hook_h <br/> # DEFINE _ wince_kb_hook_h <br/> // used for passing to setwindowshookex funtion to set a low level (LL) keyboard hook <br/> # define wh_keyboard_ll 20 <br/> // define the function types used by hooks <br/> typedef lresult (callback * hookproc) (INT code, wparam, lparam); <br/> typedef hhook (winapi * _ setwindowshookexw) (INT, hookproc, hinstance, DWORD ); <br/> typedef lresult (winapi * _ callnexthookex) (hhook, Int, wparam, lparam); <br/> typedef lresult (winapi * _ unhookwindowshookex) (hhook ); <br/> // For the low level keyboard hook, your keyboards procedures is passed a pointer to KBDLLHOOKSTRUCT instance <br/> typedef struct {<br/> DWORD vkcode; <br/> DWORD scancode; <br/> DWORD flags; <br/> DWORD time; <br/> ulong_ptr dwextrainfo; <br/>} KBDLLHOOKSTRUCT, * pkbdllhookstruct; <br/> // Win32 hook APIs <br/> static _ setwindowshookexw setwindowshookex; <br/> static _ unhookwindowshookexunhookwindowshookex; <br/> static _ callthookex callnexthookex; <br/> bool activatekbhook (hinstance, hookproc callback); <br/> bool deactivatekbhook (); <br/> hresult defhookproc (INT, wparam, lparam ); <br/> # endif <br/>

Wincekbhook. cpp

# Include "stdafx. H "<br/> # include" wincekbhook. H "<br/> // globals <br/> hinstance g_hhookapidll = NULL; // handle to coredll. DLL, where all the hook related APIs are present <br/> hhook g_hinstalledllkbdhook = NULL; // handle represents handle to the installed kb hook <br/> bool activatekbhook (hinstance, hookproc llkeyboardhookcallbackfunction) <br/>{< br/> // We need to manually Load These standard Win32 api cils <br/> // msdn states that these aren't supported in wince <br/> setwindowshookex = NULL; <br/> callnexthookex = NULL; <br/> unhookwindowshookex = NULL; <br/> // now load the coredll. DLL <br/> g_hhookapidll = loadlibrary (_ T ("coredll. DLL "); <br/> If (g_hhookapidll = NULL) <br/>{< br/> // something is awfully wrong <br/> // The dll has to be present <br/> return false; <br/>}< br/> els E <br/> {<br/> // load the setwindowshookex API call <br/> // The setwindowshookex function instils an application-defined hook procedure into a hook chain. <br/> // You wocould install a hook procedure to monitor the system for certain types of events. <br/> // here we use the hook to monitor kyeboard events <br/> setwindowshookex = (_ setwindowshookexw) getprocaddress (g_hhookapidll, _ T ("setwindowshookex W "); <br/> If (setwindowshookex = NULL) <br/>{< br/> // This means that the MS has really stopped supporting this API in wince <br/> return false; <br/>}< br/> else <br/> {<br/> // install the KB hook <br/> // The Hande needs to be saved for default Processing of the events and to uninstall the hook, once we are done with it <br/> g_hinstalledllkbdhook = setwindowshookex (wh_keyboard_ll, llkeyboardhookcallbackf Unction, hinstance, 0); <br/> If (g_hinstalledllkbdhook = NULL) <br/>{< br/> return false; <br/>}< br/> // load callnexthookex () API call <br/> // The callnexthookex function passes the hook information to the next hook procedure in the current hook chain. <br/> // we use this call for default processing of events. <br/> callnexthookex = (_ callnexthookex) getprocaddress (g_hhookapidll, _ T ("callnexthoo Kex "); <br/> If (callnexthookex = NULL) <br/>{< br/> return false; <br/>}< br/> // load unhookwindowshookex () API <br/> // The unhookwindowshookex function removes a hook procedure installed in a hook chain by the setwindowshookex function. <br/> // we use this call to unistall the hook. <br/> unhookwindowshookex = (_ unhookwindowshookex) getprocaddress (g_hhookapidll, _ T ("unhookwindowshookex"); <br/> If (unh Ookwindowshookex = NULL) <br/>{< br/> return false; <br/>}< br/> // all the APIs are loaded and the application is hooked <br/> return true; <br/>}< br/> bool deactivatekbhook () <br/> {<br/> // unload the hook <br/> If (g_hinstalledllkbdhook! = NULL) <br/>{< br/> unhookwindowshookex (g_hinstalledllkbdhook); <br/> g_hinstalledllkbdhook = NULL; <br/>}< br/> // unload the coredll. DLL <br/> If (g_hhookapidll! = NULL) <br/>{< br/> freelibrary (g_hhookapidll); <br/> g_hhookapidll = NULL; <br/>}</P> <p> // We have terminated gracefully <br/> return true; <br/>}< br/> hresult defhookproc (INT ncode, wparam, lparam) <br/>{< br/> return callnexthookex (g_hinstalledllkbdhook, ncode, wparam, lparam); <br/>}

This implementation is similar to copying a replica set on the network. I also implemented one and implemented a defhookproc function, because I am too lazy to publish the hhook used in callnexthookex. So, just call defhookproc In the hook filter, you do not need to call callnexthookex. I have provided a parameter.

Finally, I tried to implement the filter function:

Int blockkeylist [] = {vk_f1, vk_f2 }; <br/> # ifndef hc_action <br/> # define hc_action 0 <br/> # endif // hc_action <br/> lresult callback llkeyboardhookcallbackfunction (INT ncode, wparam, lparam) <br/>{< br/> KBDLLHOOKSTRUCT * phook = (KBDLLHOOKSTRUCT *) lparam; <br/> If (ncode> = hc_action) <br/>{< br/> If (wparam = wm_keydown & phook-> vkcode = vk_f1) <br/>{< br/> g_app.onkeyevent (key_event_key_press, sl_key_soft1, lparam); <br/> return 1; <br/>}< br/> else if (wparam = wm_keyup & phook-> vkcode = vk_f1) <br/>{< br/> g_app.onkeyevent (key_event_key_release, sl_key_soft1, lparam); <br/> return 1; <br/>}< br/> else if (wparam = wm_keydown & phook-> vkcode = vk_f2) <br/>{< br/> g_app.onkeyevent (key_event_key_press, sl_key_soft2, lparam); <br/> return 1; <br/>}< br/> else if (wparam = wm_keyup & phook-> vkcode = vk_f2) <br/>{< br/> g_app.onkeyevent (key_event_key_release, sl_key_soft2, lparam); <br/> return 1; <br/>}< br/> return callnexthookex (g_hinstalledllkbdhook, ncode, wparam, lparam); <br/>}

From
I have read that there should be no problem, but I found that crash was used several times. Of course, these crash are related to MessageBox. When MessageBox is popped up, the program will not respond again !!! Confused. Is MessageBox a different thread ...... %. If you want to get it, at least a bit. If g_app.onkeyevent pops up MessageBox, isn't it blocking ?? Maybe such a function does not allow you to block it here. So I decided to change it. Use postmessage to send an asynchronous message.

If (Code >=0) <br/>{< br/> hwnd; <br/> tchar sztitle [max_loadstring]; // Title Bar text <br/> tchar szwindowclass [max_loadstring]; // main window class name <br/> loadstring (g_hinst, ids_app_title, sztitle, max_loadstring ); <br/> loadstring (g_hinst, idc_scanlife, szwindowclass, max_loadstring); <br/> // if it is already running, then focus on the window, and exit <br/> hwnd = findwindow (szwindowclass, sztitle); <br/> If (hwnd) <br/>{< br/> If (phook-> vkcode = vk_f1 & wparam = wm_keydown) <br/>{< br/> postmessage (hwnd, wm_keydown, vk_f1, 0); <br/> return true; <br/>}< br/> else if (phook-> vkcode = vk_f1 & wparam = wm_keyup) <br/>{< br/> postmessage (hwnd, wm_keyup, vk_f1, 0); <br/> return true; <br/>}< br/> else if (phook-> vkcode = vk_f2 & wparam = wm_keydown) <br/>{< br/> postmessage (hwnd, wm_keydown, vk_f2, 0); <br/> return true; <br/>}< br/> else if (phook-> vkcode = vk_f2 & wparam = wm_keyup) <br/>{< br/> postmessage (hwnd, wm_keyup, vk_f2, 0); <br/> return true; <br/>}< br/> return defhookproc (Code, wparam, lparam );

Sure enough. :) of course, as I said at the beginning, the problem still persists. Some mobile phones on Smartphone directly fail to setwindowshookex! In addition to program Signature Estimation, there is no way to do this. Continue to explore anyway.

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.