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()
.
if(!ActivateKBHook(hInstance, LLKeyboardHookCallbackFunction))
{
MessageBox(GetActiveWindow(),
TEXT("Couldn't intall hook...Terminating"),
TEXT("Warning"), NULL);
exit(1);
}
LRESULT CALLBACK LLKeyboardHookCallbackFunction( int nCode,
WPARAM wParam, LPARAM lParam )
{
if(((((KBDLLHOOKSTRUCT*)lParam)->vkCode) == VK_LEFT) ||
((((KBDLLHOOKSTRUCT*)lParam)->vkCode) == VK_RIGHT))
{
keybd_event(VK_UP, 0, 0, 0);
keybd_event(VK_UP, 0, KEYEVENTF_KEYUP, 0);
}
return CallNextHookEx(g_hInstalledLLKBDhook, nCode, wParam, lParam);
}
DeactivateKBHook();