WinCE想实现键盘勾子,没有Windows那么简单。当然,也还是可以实现的:
winceKBhook.h
#ifndef _WINCE_KB_HOOK_H
#define _WINCE_KB_HOOK_H
//used for passing to SetWindowsHookEx funtion to set a Low level (LL) keyboard hook
#define WH_KEYBOARD_LL 20
#define WINCEKBHOOK_API extern "C" __declspec(dllexport)
// Define the function types used by hooks
typedef LRESULT (CALLBACK* HOOKPROC)(int code, WPARAM wParam, LPARAM lParam);
typedef HHOOK (WINAPI *_SetWindowsHookExW)(int, HOOKPROC, HINSTANCE, DWORD);
typedef LRESULT (WINAPI *_CallNextHookEx)(HHOOK, int, WPARAM, LPARAM);
typedef LRESULT (WINAPI *_UnhookWindowsHookEx)(HHOOK);
// For the low level keyboard hook, your keyboards procedures is passed a pointer to KBDLLHOOKSTRUCT instance
typedef struct {
DWORD vkCode;
DWORD scanCode;
DWORD flags;
DWORD time;
ULONG_PTR dwExtraInfo;
} KBDLLHOOKSTRUCT, *PKBDLLHOOKSTRUCT;
// Win32 Hook APIs
static _SetWindowsHookExW SetWindowsHookEx;
static _UnhookWindowsHookEx UnhookWindowsHookEx;
static _CallNextHookEx CallNextHookEx;
//#if defined (__cplusplus)
//extern "C" {
//#endif
// Loads the keyboard hook.
WINCEKBHOOK_API BOOL ActivateKBHook(HINSTANCE hInstance, HOOKPROC KeyboardHookCallbackFunc);
// Uninstall the KB hook
WINCEKBHOOK_API BOOL DeactivateKBHook();
//#if defined (__cplusplus)
//}
//#endif
#endif
winceKBhook.cpp
#include <windows.h>
#include "winceKBhook.h"
//globals
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
// WINCEKBHOOK_API bool StartHook(HINSTANCE hInstance)
// {
// ActivateKBHook(hInstance, LLKeyboardHookCallbackFunction);
// return true;
// }
/**
* Function Name:ActivateKBHook
*
* Function Desc:Initializes the proc. adress of various hook related APIs.
* Loads the keyboard hook.
*
* Parameters:
* HINSTANCE hInstance : handle to the application to be hooked
* HOOKPROC LLKeyboardHookCallbackFunction : procedure where the control will come to after any KB event.
* Returns:
* true if we get all the proc addresses of hook related APIs and load the hook succesfully
* false if any of the above fails
**/
WINCEKBHOOK_API BOOL ActivateKBHook(HINSTANCE hInstance, HOOKPROC KeyboardHookCallbackFunc)
{
//we need to manually load these standard Win32 API calls
//MSDN states that these aren't supported in WinCE
SetWindowsHookEx = NULL;
CallNextHookEx = NULL;
UnhookWindowsHookEx = NULL;
//now load the coredll.dll
g_hHookApiDLL = LoadLibrary(_T("coredll.dll"));
if(g_hHookApiDLL == NULL)
{
//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
SetWindowsHookEx = (_SetWindowsHookExW)GetProcAddress(g_hHookApiDLL, _T("SetWindowsHookExW"));
if(SetWindowsHookEx == NULL)
{
//this means that MS has really stopped supporting this API in WinCE
return false;
}
else
{
//install the KB hook
//the hande needs to be saved for default processing of the events and to uninstall the hook, once we are done with it
g_hInstalledLLKBDhook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookCallbackFunc, hInstance, 0);
if(g_hInstalledLLKBDhook == NULL)
{
return false;
}
}
//load CallNextHookEx() API call
//the CallNextHookEx function passes the hook information to the next hook procedure in the current hook chain.
//we use this call for default processing of events.
CallNextHookEx = (_CallNextHookEx)GetProcAddress(g_hHookApiDLL, _T("CallNextHookEx"));
if(CallNextHookEx == NULL)
{
return false;
}
//load UnhookWindowsHookEx() API
//the UnhookWindowsHookEx function removes a hook procedure installed in a hook chain by the SetWindowsHookEx function.
//we use this call to unistall the hook.
UnhookWindowsHookEx = (_UnhookWindowsHookEx)GetProcAddress(g_hHookApiDLL, _T("UnhookWindowsHookEx"));
if(UnhookWindowsHookEx == NULL)
{
return false;
}
}
//all the APIs are loaded and the application is hooked
return true;
}
// Uninstall the KB hook
WINCEKBHOOK_API BOOL DeactivateKBHook()
{
//unload the hook
if(g_hInstalledLLKBDhook != NULL)
{
UnhookWindowsHookEx(g_hInstalledLLKBDhook);
g_hInstalledLLKBDhook = NULL;
}
//unload the coredll.dll
if(g_hHookApiDLL != NULL)
{
FreeLibrary(g_hHookApiDLL);
g_hHookApiDLL = NULL;
}
//we have terminated gracefully
return true;
}
具体用法:
//开启钩子
ActivateKBHook(theApp.m_hInstance, KeyboardProc);
//关闭钩子
DeactivateKBHook();
// Param[in]: code: hook code
// Param[in]: wParam: virtual-key code
// Param[in]: lParam: keystroke-message information
LRESULT CALLBACK KeyboardProc(int code, WPARAM wParam, LPARAM lParam)
{
static bool FirstValue = true;//0为假,非0为真
//
if(wParam==256 && FirstValue) //256:WM_KEYDOWN, 257:WM_KEYUP
{
FirstValue = false;
//::PostMessage(g_hWnd, MSG_PROC_KBHOOK, ((KBDLLHOOKSTRUCT*)lParam)->vkCode, NULL);
//return 1;//返回1表示已经处理了该消息,不再转发给操作系统
}
if (wParam == 91)//Win键
{
return 1;
}
//
if(wParam == 257)
{
FirstValue = true;
}
return CallNextHookEx((HHOOK)KeyboardProc, code, wParam, lParam);
}