|
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().
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();
| You must Sign In to use this message board. |
|
| | Msgs 1 to 25 of 25 (Total in Forum: 25) (Refresh) | FirstPrevNext |
|
 |
|
|
Hi and thanks a lot for the informative article. I tried it and it works fine, but I noticed that I will not get key events when the focus is not on my application. That of course is normal, but my question is how to make this hook global, so that all key events are caought, even though input focus is lost?
|
| Sign In·View Thread·PermaLink | 1.00/5 (2 votes) |
|
|
|
 |
|
|
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.
Shashikant
|
| Sign In·View Thread·PermaLink | 1.00/5 (2 votes) |
|
|
|
 |
|
|
Hello Sir,
I had gone through your above article. It is very informative and helpful. Thank you for posting such article.
Well, I have to code a hook in WinCE using SetWinEventHook.
I had tried your explained method as above, but not able to create hook. I am getting following errors - Generating Code... Compiling resources... Linking... MainFrm.obj : error LNK2019: unresolved external symbol "struct HWINEVENTHOOK__ * __cdecl SetWinEventHook(unsigned long,unsigned long,struct HINSTANCE__ *,void (__cdecl*)(struct HWINEVENTHOOK__ *,unsigned long,struct HWND__ *,long,long,unsigned long,unsigned long),unsigned long,unsigned long,unsigned long)" (?SetWinEventHook@@YAPAUHWINEVENTHOOK__@@KKPAUHINSTANCE__@@P6AXPAU1@KPAUHWND__@@JJKK@ZKKK@Z) referenced in function "protected: int __cdecl CMainFrame::OnCreate(struct tagCREATESTRUCTW *)" (?OnCreate@CMainFrame@@IAAHPAUtagCREATESTRUCTW@@@Z) Windows Mobile 5.0 Pocket PC SDK (ARMV4I)\Release/ScureApp.exe : fatal error LNK1120: 1 unresolved externals
Please have a quick look on my code as follows - my MainFrm :: OnCreate() contains
g_hHkApiDLL = LoadLibrary(_T("user32.dll")); g_hHkApiDLLBox = LoadLibrary(_T("coredll.dll"));
if((g_hHkApiDLLBox && g_hHkApiDLL) == NULL) { AfxMessageBox(_T("Error to load coredll.dll")); //something is awfully wrong //the dll has to be present return false; } else { //load the SetWindowsHookEx API call //the SetWindowsHookEx function installs an application-defined hook procedure into a hook chain. //You would install a hook procedure to monitor the system for certain types of events. //here we use use the hook to monitor kyeboard events LPTSTR pSetWinEventHook; pSetWinEventHook = (LPTSTR)GetProcAddress(g_hHkApiDLLBox, _T("SetWinEventHook")); if(pSetWinEventHook == NULL) { AfxMessageBox(_T("Error to install hook procedure")); //this means that MS has really stopped supporting this API in WinCE return false; } else { //SetWindowRetProcHook (TRUE); // hEvtHook = SetWinEventHook (EVENT_MIN, EVENT_MAX, hEvtHook = SetWinEventHook (EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, NULL, ForegroundProc, 0, 0, WINEVENT_OUTOFCONTEXT); } }
My ForgroundProc as follows -
VOID CALLBACK ForegroundProc( HWINEVENTHOOK hWinEventHook, DWORD event, HWND hwnd,LONG idObject, LONG idChild, DWORD dwEventThread, DWORD dwmsEventTime ) { AfxMessageBox(_T("Inside ForegroundProc")); TCHAR buf[MAX_PATH];
HWND hWndForeGroundWin = GetForegroundWindow(); if (GetClassName(hWndForeGroundWin, buf, MAX_PATH)) { if (!_tcsicmp (buf, TEXT("IEFRAME")) || _tcsstr (buf, TEXT("Internet Explorer")) || !_tcsicmp (buf, TEXT("MozillaUIWindowClass"))) { AfxMessageBox(TEXT("Found Internet Explorer_Server")); } } }
Also we have declared in WinceKBkook.h file - HINSTANCE g_hHookApiDLL = NULL; //handle to coredll.dll, where all the hook related APIs are present HHOOK g_hInstalledLLKBDhook = NULL; //g_hInstalledLLKBDhook represents handle to the installed KB hook #define EVENT_SYSTEM_FOREGROUND 0x0003 #define WINEVENT_OUTOFCONTEXT 0x0000 // Events are ASYNC
typedef VOID (CALLBACK* WINEVENTPROC)( HWINEVENTHOOK hWinEventHook, DWORD event, HWND hwnd, LONG idObject, LONG idChild, DWORD idEventThread, DWORD dwmsEventTime);
//typedef HWINEVENTHOOK (WINAPI *SetWinEventHook)(DWORD, DWORD, HMODULE, WINEVENTPROC, DWORD, DWORD, DWORD);
//extern "C" WINUSERAPI HWINEVENTHOOK WINAPI SetWinEventHook( __in DWORD eventMin, __in DWORD eventMax, __in_opt HMODULE hmodWinEventProc, __in WINEVENTPROC pfnWinEventProc, __in DWORD idProcess, __in DWORD idThread, __in DWORD dwFlags);
Now request you, please look into the code, and suggest me where I am getting wrong.
Thanks.
Regards,
Shashikant
|
| Sign In·View Thread·PermaLink | 1.33/5 (2 votes) |
|
|
|
 |
|
|
Hi,
i am new to wince. I followed the steps from the following docs but i did not succeed.
http://linuxtogo.org/~koen/letux/bootloader/bsp/DOC/DotNET420Guide.html http://linuxtogo.org/~koen/letux/doc/NET410Guide.htm http://arm.co.kr/programs/read.php?board=qna&uid=409&cp=3&field=&keyWord=&PHPSESSID=c20fb1f1438ba974d7c2beca9a4c89c2 http://www.embedinfo.com/english/download/winces3ceb2410.pdf
if any one knows how to port wince to smdk2410 board please help me.
amiya kumar das
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Thanks man your code work really fine. I'll give the 5. Well documented, and simple. Many thanks.
Dr.Luiji
Trust and you'll be trusted.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
when codes run to g_hInstalledLLKBDhook = SetWindowsHookEx(WH_KEYBOARD_LL, LLKeyboardHookCallbackFunction, hInstance, 0);
it always ruturn NULL,and the error code is 5, it seems Access denied error.Please help.
|
| Sign In·View Thread·PermaLink | 1.89/5 (9 votes) |
|
|
|
 |
|
|
 |
|
|
The KB Hook is very useful But..Could I get the other message? I want to get the MOUSE Hook, but I can not use the method to get the message Could you tell me which way I can get the message?? Thanx~
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
|
I want to record the mouse action (position, click and drap) to a script and to re-play the script in Win-CE
or any other solution for the requirement? thank you~
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi
I am new to the winCE enviornment. In my application I have to capture the Keyboards events and mouse events and than playback. I am able to trap the Keyboards events but unable to succed with Mouse events. Could you help me with the ways I can capture the mouse events on winCE and than Playback.
Shishir
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
|
Hi~Shishir
I am new, too;P I use the following functions: EnumWindows: to find the window handle GetWindowLong: to add a my defined process any issue, welcome to discuss ^_^
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi, I am a WinCE 4.2 based application developer. I have to trap all global mouse and virtual keyboard events in an applicationj. Can anybody tell me How to do that. It's very urgent.
A lot of thanks for any HELP.
Thanks, Rahul 
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi,
I am using your code in a dialog based application. But I am getting compilation error.
winceKBhook.h(47) : error C2501: 'WINCEKBHOOK_API' : missing storage-class or type specifiers
where WINCEKBHOOK_API has been defined? I just added your two files. Am I missing something? Can you post a sample application. It will help a lot.
Thanks in advance.
With regards A.Santosh
-- modified at 2:39 Tuesday 23rd May, 2006
|
| Sign In·View Thread·PermaLink | 2.29/5 (4 votes) |
|
|
|
 |
|
|
 |
|
|
Hi,
i'm using your winceKBhook functions on IPAQ6515 ( evc++ 4.0 ) and all seems work correctly.
I have only a problem which is to disable the Contacts Manager Button ( 0xCA ) and the Messages Manager Button ( 0xCB ). I used this code for other two keys ( Make a Call 0x72 - Drop a Call 0x73 ) and works only for these ones. --------------------------------------------------------------------------- if( K_CODE == 0x72 || K_CODE == 0x73 || K_CODE == 0xCA || K_CODE == 0xCB ) { // Disable KeyBoard functions EnableHardwareKeyboard(FALSE);
//Generate the keyboard press event of the mapped key keybd_event(0, 0, 0, 0);
//release the mapped key keybd_event(0, 0, KEYEVENTF_KEYUP, 0); } ---------------------------------------------------------------------------
thanks Vittorio
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi,
i'm developing a simple application on IPAQ6515 ( evc++ 4.0 ). My first scope is to disable some keys and i used the LLKeyboardHookCallbackFunction to catch each key pressing. After the HookActivation all work great but when return, CallNextHookEx(g_hInstalledLLKBDhook, nCode,wParam, lParam) give me always an access violation 
a typical situation is like this:
g_hInstalledLLKBDhook = 1 nCode = 0 wParam = 738393240 lParam = 256
thanks
|
| Sign In·View Thread·PermaLink | 4.40/5 (2 votes) |
|
|
|
 |
|
|
Hi there!
I am assuming that you get an access violation when the flow of control is in the low level callback function.
MSDN info starts------------- When the call is in this function....
*int nCode = Specifies a code the hook procedure uses to determine how to process the message.
*WPARAM wParam = Specifies the identifier of the keyboard message.This parameter can be one of the following messages: WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, or WM_SYSKEYUP.
*LPARAM lParam = Pointer to a KBDLLHOOKSTRUCT structure. MSDN info ends-------------
So I think the access violation is mailnly due to the manipulations that you are doing. So if possible can you post the code where the acces violation occurs.
Cheers! PK
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
..You think good..fortunately i solved the problem!
the error was that i put the LLKeyboardHookCallbackFunction in the cpp which defines the entry point for the application, so CallNextHookEx was NULL!
Now all work greatly!
thanks a lot for your help
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Could you, please, tell more about this issue, I am a beginner and don't know how to solve it.
Thanks.
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
|
Hi Prathamesh,
I'd like to ask your permission to reuse this code in a piece of sample code which will be available in a public SDK. We will keep your name in the source if you would like it to stay there.
steven
|
| Sign In·View Thread·PermaLink | 2.40/5 (5 votes) |
|
|
|
 |
|
|
Surely you can use the code by keeping my name. But please also send me some more details about the SDK. Thanks.
|
| Sign In·View Thread·PermaLink | 4.00/5 (3 votes) |
|
|
|
 |
|
|
 |
|
|
Infact I wanted to simulate the pressing and releasing of an up arrow key and hence one for the press and one for release of the up key. But it can be done with with just one. But I definitely didnt want keybd_event(VK_DOWN, 0, 0, 0). Also I should have taken care of the fact that control will come twice to this function whenever a key is pressed and release, once for each.
|
| Sign In·View Thread·PermaLink | 2.67/5 (3 votes) |
|
|
|
 |
|
|
 |
|
|
General News Question Answer Joke Rant Admin
|