Use keyboard hooks on Windows Mobile

Source: Internet
Author: User

Use keyboard hooks on Windows Mobile

The latest project involves the hook operation of the wince button. You need to block the hanging key and some system buttons.
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.
First, define some parameters.
# Define wh_keyboard_ll 20
# Define hc_action 0
Typedef lresult (callback * hookproc) (INT code, wparam, 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;
Static _ callnexthookex;

Then the 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;
}

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, 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 );
}
Finally, there is another key point. Some people on the Internet say that wh_keyboard_ll can implement global hooks without using DLL, but I found that at least on Windows Mobile machines, we still need to use DLL to implement keyboard hooks.

The above methods can be used normally in Windows Mobile 5.0 and 6.0. If you only want to block all the buttons, you can simply use the enablehardwarekeyboard function.

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.