5,693,062 members and growing! (18,102 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Mobile Development » General     Intermediate

Using keyboard hooks in WinCE

By Prathamesh S Kulkarni

The article shows how to use keyboard hooks in WinCE.
C++, C, eVC 3.0, eVC 4.0, eVC, Mobile, Windows, CE 3.0, Win Mobile, CE .NET 4.0, CE .NET 4.1, CE .NET 4.2, Visual Studio, Dev

Posted: 1 Nov 2005
Updated: 1 Nov 2005
Views: 47,725
Bookmarked: 21 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
12 votes for this Article.
Popularity: 4.66 Rating: 4.32 out of 5
1 vote, 8.3%
1
0 votes, 0.0%
2
1 vote, 8.3%
3
2 votes, 16.7%
4
8 votes, 66.7%
5

Introduction

The article shows 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 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))
{
    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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Prathamesh S Kulkarni



Location: India India

Other popular Mobile Development articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 25 (Total in Forum: 25) (Refresh)FirstPrevNext
QuestionA Global Hook?memberakafazov5:55 14 Aug '08  
GeneralApplications does not follow Emulator keyboard.memberShashi.Shinde1:44 25 Apr '08  
GeneralWinCE hooks using SetWinEventHookmemberShashi.Shinde20:30 23 Apr '08  
Questionproblem in porting wince to smdk 2410 board.memberamiya das22:47 30 Mar '08  
GeneralYou are Rockmember Dr.Luiji 7:58 13 Mar '08  
Questionwhy SetWindowsHookEx always return NULLmemberxingtian71322:41 13 May '07  
Questionhi...i Need Mouse Hook for WinCEmemberAsia_Mouse0:26 2 Aug '06  
GeneralOther hook message?memberChang Yin-Tse21:54 18 Jun '06  
QuestionRe: Other hook message?memberPrathamesh S Kulkarni20:47 20 Jun '06  
AnswerRe: Other hook message?memberChang Yin-Tse17:54 27 Jun '06  
QuestionRe: Other hook message?memberShishirAgarwal20:38 26 Jul '06  
AnswerRe: Other hook message?memberChang Yin-Tse17:44 10 Aug '06  
GeneralRe: Other hook message?memberRahul P. Shukla2:39 2 Jul '07  
GeneralGetting compilation error [modified]membersantu21:38 22 May '06  
AnswerRe: Getting compilation error [modified]memberPrathamesh S Kulkarni16:50 24 May '06  
GeneralDisable Contacts - Messages ButtonsmemberVittorio Iezzoni0:04 8 May '06  
GeneralAccess Violationmemberv_iezzoni@email.it0:45 21 Feb '06  
AnswerRe: Access ViolationmemberPrathamesh S Kulkarni8:32 21 Feb '06  
GeneralRe: Access Violationmemberv_iezzoni@email.it21:51 21 Feb '06  
GeneralRe: Access Violationmemberseryi704:44 13 Jun '06  
GeneralUse of this code in sample code in a public SDKmembercodeweakling15:08 9 Dec '05  
AnswerRe: Use of this code in sample code in a public SDKmemberPrathamesh S Kulkarni6:17 11 Dec '05  
QuestionTypo?memberJohann Gerell21:18 1 Nov '05  
AnswerRe: Typo?memberPrathamesh S Kulkarni10:04 2 Nov '05  
GeneralRe: Typo?memberJohann Gerell22:26 2 Nov '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 1 Nov 2005
Editor: Rinish Biju
Copyright 2005 by Prathamesh S Kulkarni
Everything else Copyright © CodeProject, 1999-2008
Web20 | Advertise on the Code Project