Click here to Skip to main content
11,490,378 members (57,172 online)
Click here to Skip to main content

Using keyboard hooks in WinCE

, 1 Nov 2005 CPOL 106.2K 949 38
Rate this:
Please Sign up or sign in to vote.
The article shows how to use keyboard hooks in WinCE.


The article shows how to use keyboard hooks in WinCE.


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 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 were present in coredll.lib. So, I thought of manually loading these APIs and using them. Initially, I did have some problems doing this, but a look into winuser.h in VC++, made the job a lot easier. Googling also helped me to some extent, but I don't remember the URLs now, 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 ActivateKBHook() in winceKBhook.cpp. This function loads the necessary hook APIs and installs the keyboard hook. It is necessary to pass the handle of the application to be hooked and a low level keyboard 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 can 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))
        TEXT("Couldn't intall hook...Terminating"), 
        TEXT("Warning"), NULL);

//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.


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


About the Author

No Biography provided

Comments and Discussions

Generalutility to record mouse movement and keyboard Pin
Member 1096512422-Jul-14 22:33
memberMember 1096512422-Jul-14 22:33 
Suggestion Pin
Member 1093440018-Jul-14 3:56
memberMember 1093440018-Jul-14 3:56 
QuestionIt works on WINCE6.0, but the keycode always 0? Pin
selongs10-Sep-09 5:40
memberselongs10-Sep-09 5:40 
Generalis it work on windows mobile 5 or above Pin
zeroonea25-Jun-09 21:15
memberzeroonea25-Jun-09 21:15 
GeneralStill a great article Pin
hjgode15-May-09 20:13
memberhjgode15-May-09 20:13 
QuestionIndependant hooks? Pin
JDBP17-Mar-09 6:26
memberJDBP17-Mar-09 6:26 
AnswerRe: Independant hooks? Pin
hjgode15-May-09 20:17
memberhjgode15-May-09 20:17 
QuestionA Global Hook? Pin
akafazov14-Aug-08 5:55
memberakafazov14-Aug-08 5:55 
GeneralApplications does not follow Emulator keyboard. Pin
Shashi.Shinde25-Apr-08 1:44
memberShashi.Shinde25-Apr-08 1:44 
Hello Prathmesh,

Its is great to succeed a keyboard hook on Windows Mobile 5.0 Pocket PC Emulator and using your posted code.

Thank you very much.

But, I found that whenever I use my desktop keyboard, application works fine, but whenever I use Emulator keyboard, it does not work.
So request you, please guide me to trap Emulator keyboard events.

I had used same code as per your article.

Thank you.

GeneralWinCE hooks using SetWinEventHook Pin
Shashi.Shinde23-Apr-08 20:30
memberShashi.Shinde23-Apr-08 20:30 
Questionproblem in porting wince to smdk 2410 board. Pin
amiya das30-Mar-08 22:47
memberamiya das30-Mar-08 22:47 
GeneralYou are Rock Pin
Dr.Luiji 13-Mar-08 7:58
member Dr.Luiji 13-Mar-08 7:58 
Questionwhy SetWindowsHookEx always return NULL Pin
xingtian71313-May-07 22:41
memberxingtian71313-May-07 22:41 
Questionhi...i Need Mouse Hook for WinCE Pin
Asia_Mouse2-Aug-06 0:26
memberAsia_Mouse2-Aug-06 0:26 
QuestionOther hook message? Pin
Chang Yin-Tse18-Jun-06 21:54
memberChang Yin-Tse18-Jun-06 21:54 
AnswerRe: Other hook message? Pin
Prathamesh S Kulkarni20-Jun-06 20:47
memberPrathamesh S Kulkarni20-Jun-06 20:47 
AnswerRe: Other hook message? Pin
Chang Yin-Tse27-Jun-06 17:54
memberChang Yin-Tse27-Jun-06 17:54 
QuestionRe: Other hook message? Pin
ShishirAgarwal26-Jul-06 20:38
memberShishirAgarwal26-Jul-06 20:38 
AnswerRe: Other hook message? Pin
Chang Yin-Tse10-Aug-06 17:44
memberChang Yin-Tse10-Aug-06 17:44 
GeneralRe: Other hook message? Pin
Rahul P. Shukla2-Jul-07 2:39
memberRahul P. Shukla2-Jul-07 2:39 
GeneralGetting compilation error [modified] Pin
santu22-May-06 21:38
membersantu22-May-06 21:38 
AnswerRe: Getting compilation error [modified] Pin
Prathamesh S Kulkarni24-May-06 16:50
memberPrathamesh S Kulkarni24-May-06 16:50 
GeneralDisable Contacts - Messages Buttons Pin
Vittorio Iezzoni8-May-06 0:04
memberVittorio Iezzoni8-May-06 0:04 
GeneralAccess Violation Pin
v_iezzoni@email.it21-Feb-06 0:45
memberv_iezzoni@email.it21-Feb-06 0:45 
AnswerRe: Access Violation Pin
Prathamesh S Kulkarni21-Feb-06 8:32
memberPrathamesh S Kulkarni21-Feb-06 8:32 
GeneralRe: Access Violation Pin
v_iezzoni@email.it21-Feb-06 21:51
memberv_iezzoni@email.it21-Feb-06 21:51 
GeneralRe: Access Violation Pin
seryi7013-Jun-06 4:44
memberseryi7013-Jun-06 4:44 
GeneralUse of this code in sample code in a public SDK Pin
codeweakling9-Dec-05 15:08
membercodeweakling9-Dec-05 15:08 
AnswerRe: Use of this code in sample code in a public SDK Pin
Prathamesh S Kulkarni11-Dec-05 6:17
memberPrathamesh S Kulkarni11-Dec-05 6:17 
QuestionTypo? Pin
Johann Gerell1-Nov-05 21:18
memberJohann Gerell1-Nov-05 21:18 
AnswerRe: Typo? Pin
Prathamesh S Kulkarni2-Nov-05 10:04
memberPrathamesh S Kulkarni2-Nov-05 10:04 
GeneralRe: Typo? Pin
Johann Gerell2-Nov-05 22:26
memberJohann Gerell2-Nov-05 22:26 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 1 Nov 2005
Article Copyright 2005 by Prathamesh S Kulkarni
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid