Click here to Skip to main content
6,630,901 members and growing! (17,121 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, Windows, Mobile, Visual Studio, Dev
Posted:1 Nov 2005
Views:63,674
Bookmarked:30 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
13 votes for this article.
Popularity: 4.88 Rating: 4.38 out of 5
1 vote, 7.7%
1

2
1 vote, 7.7%
3
2 votes, 15.4%
4
9 votes, 69.2%
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


Member

Location: India India

Other popular Mobile Development articles:

  • Writing Your Own GPS Applications: Part 2
    In part two of the series, the author of "GPS.NET" teaches developers how to write GPS applications suitable for the real world by mastering GPS precision concepts. Source code includes a working NMEA interpreter and sample high-precision application in C# and VB.NET.
  • Writing Your Own GPS Applications: Part I
    What is it that GPS applications need to be good enough to use for in-car navigation? Also, how does the process of interpreting GPS data actually work? In this three-part series, I will cover both topics and give you the skills you need to write a commercial-grade GPS application.
  • Learn How to Find GPS Location on Any SmartPhone, and Then Make it Relevant
    A step by step tutorial for getting GPS from any SmartPhone, even without GPS built in, and then making location useful.
  • iPhone UI in Windows Mobile
    It's an interface that works with transparency effects. As a sample I used an interface just like the iPhone one. In this tutorial I am explaining how simple is working with transparency on Windows Mobile.
  • Pocket 1945 - A C# .NET CF Shooter
    An article on Pocket PC game development
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 30 (Total in Forum: 30) (Refresh)FirstPrevNext
GeneralIt works on WINCE6.0, but the keycode always 0? Pinmemberselongs5:40 10 Sep '09  
Generalis it work on windows mobile 5 or above Pinmemberzeroonea21:15 25 Jun '09  
GeneralStill a great article Pinmemberhjgode20:13 15 May '09  
QuestionIndependant hooks? PinmemberJDBP6:26 17 Mar '09  
AnswerRe: Independant hooks? Pinmemberhjgode20:17 15 May '09  
QuestionA Global Hook? Pinmemberakafazov5:55 14 Aug '08  
GeneralApplications does not follow Emulator keyboard. PinmemberShashi.Shinde1:44 25 Apr '08  
GeneralWinCE hooks using SetWinEventHook PinmemberShashi.Shinde20:30 23 Apr '08  
Questionproblem in porting wince to smdk 2410 board. Pinmemberamiya das22:47 30 Mar '08  
GeneralYou are Rock Pinmember Dr.Luiji 7:58 13 Mar '08  
Questionwhy SetWindowsHookEx always return NULL Pinmemberxingtian71322:41 13 May '07  
Questionhi...i Need Mouse Hook for WinCE PinmemberAsia_Mouse0:26 2 Aug '06  
GeneralOther hook message? PinmemberChang Yin-Tse21:54 18 Jun '06  
QuestionRe: Other hook message? PinmemberPrathamesh S Kulkarni20:47 20 Jun '06  
AnswerRe: Other hook message? PinmemberChang Yin-Tse17:54 27 Jun '06  
QuestionRe: Other hook message? PinmemberShishirAgarwal20:38 26 Jul '06  
AnswerRe: Other hook message? PinmemberChang Yin-Tse17:44 10 Aug '06  
GeneralRe: Other hook message? PinmemberRahul P. Shukla2:39 2 Jul '07  
GeneralGetting compilation error [modified] Pinmembersantu21:38 22 May '06  
AnswerRe: Getting compilation error [modified] PinmemberPrathamesh S Kulkarni16:50 24 May '06  
GeneralDisable Contacts - Messages Buttons PinmemberVittorio Iezzoni0:04 8 May '06  
GeneralAccess Violation Pinmemberv_iezzoni@email.it0:45 21 Feb '06  
AnswerRe: Access Violation PinmemberPrathamesh S Kulkarni8:32 21 Feb '06  
GeneralRe: Access Violation Pinmemberv_iezzoni@email.it21:51 21 Feb '06  
GeneralRe: Access Violation Pinmemberseryi704:44 13 Jun '06  

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-2009
Web21 | Advertise on the Code Project