Click here to Skip to main content
15,886,110 members
Articles / Mobile Apps / Windows Mobile

Keyboard hooks in WinCE

Rate me:
Please Sign up or sign in to vote.
3.61/5 (7 votes)
3 Nov 2005CPOL1 min read 98.1K   927   38   16
An article on using keyboard hooks in WinCE.

Introduction

The article shows you how to use keyboard hooks in WinCE.

Background

I came across a problem where I had to remap certain special keys on my handheld for an existing dialog based application. The only solution that I knew that would gracefully do this in WinCE was hooks. But MSDN states that hook APIs aren't supported in WinCE. But I found that they are present in the coredll.lib. So I thought about manually loading these APIs and using it. Initially I did have some problem doing this, but a look into winuser.h in VC++, made the job a lot easier. Also Googling helped me to some extent, but I don't remember the URLs now. So my apologies to those whom I haven't given the credit.

Using the code

You just have to use two files winceKBhook.cpp and winceKBhook.h. The code has been commented thoroughly for easy understanding. You can use these files in either an exe or a DLL.

The mechanism of using a hook would be to install it. This is done using the function in winceKBhook.cpp, ActivateKBHook(). This function loads the necessary hook APIs and installs the KB hook. It is necessary to pass the handle of the application to be hooked and a low level KB procedure, which has to be defined by the user. All the keyboard events come to this procedure. Your code can then manage these events the way you want. The example shown below just remaps the keys. After you are done with using the hook, you unload it using DeActivateKBHook().

//Install the KB hook by passing the handle of the application to be hooked 
//and the address of the KB procedure which will handle all the KB events
if(!ActivateKBHook(hInstance, LLKeyboardHookCallbackFunction))
{
    MessageBox(GetActiveWindow(), 
        TEXT("Couldn't intall hook...Terminating"), 
        TEXT("Warning"), NULL);
    exit(1);
}

//LLKeyboardHookCallbackFunction is the funtion 
//whose address we passed to the system while installing the hook.
//so all the KB events will bring the control to this procedure.
//Here we want that when the user presse left 
//or right key it should be interpreted as an UP key
//so now you can allow the user to configure 
//the key boards the way he/she wants it
LRESULT CALLBACK LLKeyboardHookCallbackFunction( int nCode, 
                              WPARAM wParam, LPARAM lParam ) 
{
    if(((((KBDLLHOOKSTRUCT*)lParam)->vkCode) == VK_LEFT) || 
      ((((KBDLLHOOKSTRUCT*)lParam)->vkCode) == VK_RIGHT))
    {
        //Generate the keyboard press event of the mapped key
        keybd_event(VK_UP, 0, 0, 0); 

        //release the mapped key
        keybd_event(VK_UP, 0, KEYEVENTF_KEYUP, 0); 
    }

    //let default processing take place
    return CallNextHookEx(g_hInstalledLLKBDhook, nCode, wParam, lParam);
}


//we are done with the hook. now uninstall it.
DeactivateKBHook();

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalflags argument is always 0 Pin
Greggee13-Aug-09 11:10
Greggee13-Aug-09 11:10 
Questionprefetch abort Pin
Paul Heil27-Oct-08 12:54
Paul Heil27-Oct-08 12:54 
AnswerRe: prefetch abort Pin
sford_1111916-May-10 20:43
sford_1111916-May-10 20:43 
GeneralEasy way to show and hide virtual keyboard Pin
Thuy NH21-Apr-08 15:58
Thuy NH21-Apr-08 15:58 
GeneralWH_KEYBOARD_LL Pin
FuckMe22-Aug-07 13:03
FuckMe22-Aug-07 13:03 
QuestionScreensaver application on Windows CE (5.0 version) Pin
hnim16-Aug-07 22:58
hnim16-Aug-07 22:58 
QuestionHow to install System Wide Hook? Pin
Navin C11-Jun-07 6:12
Navin C11-Jun-07 6:12 
Questionhow to set virtual keyboard hooks in WinCE Pin
doglovecat52728-Mar-07 21:53
doglovecat52728-Mar-07 21:53 
AnswerRe: how to set virtual keyboard hooks in WinCE Pin
Rahul P. Shukla1-Jul-07 20:06
Rahul P. Shukla1-Jul-07 20:06 
GeneralStatic delcarations in winceKBhook.h Pin
Damon8or29-Sep-06 7:13
Damon8or29-Sep-06 7:13 
Questionhi can you translate it into vb.net or C#? Pin
helpme_2k219-Jun-06 22:33
helpme_2k219-Jun-06 22:33 
I am happy to see your sample.
I have tried to go through so many code (involve hotkey) but none of them works well for me.
Generalsuggestion Pin
cjwn12-Mar-06 6:58
cjwn12-Mar-06 6:58 
QuestionCan I use your code in sample code for a public SDK Pin
codeweakling12-Dec-05 7:31
codeweakling12-Dec-05 7:31 
AnswerRe: Can I use your code in sample code for a public SDK Pin
Prathamesh S Kulkarni12-Dec-05 16:17
Prathamesh S Kulkarni12-Dec-05 16:17 
GeneralThere is some important information missing Pin
xryl6693-Nov-05 22:56
xryl6693-Nov-05 22:56 
AnswerRe: There is some important information missing Pin
Prathamesh S Kulkarni11-Dec-05 5:22
Prathamesh S Kulkarni11-Dec-05 5:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.