此帖出自嵌入式系统论坛
最新回复
#include "stdafx.h"
#include "winceKBhook.h"
#include "IconButton.h"
//globals
#define PARENT_WND CHomeScreenDlg
#define POWER_ON 0
#define POWER_OFF 1
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
static int begin=0,beginTime=0,EndTime=0;
static int PowerOnOff=0;
HWND hWnd;
/**
* 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
*
* Author: Prathamesh Kulkarni
**/
LRESULT CALLBACK hwKeyBdHook(int code, WPARAM wParam, LPARAM lParam)
{
CWinThread* pThread;
// LPMSG lpMsg;
PKBDLLHOOKSTRUCT lpKBD;
CIconButton* pIconBtn;
//pIconBtn = new CIconButton(GetSafeHwnd(), WM_MSG_ICONCLICK, 1, 1);
if ((code < 0 /*&& code != MSGF_DDEMGR*/) ||
(pThread = AfxGetThread()) == NULL)
{
return ::CallNextHookEx(g_hInstalledLLKBDhook,
code, wParam, lParam);
}
ASSERT(pThread != NULL);
HANDLE p_evtSignal;
p_evtSignal = CreateEvent(NULL, FALSE, FALSE, TEXT("UserInputEvent"));
//p_evtSignal = CreateEvent(NULL, FALSE, FALSE, szevtBacklightChange);
if(p_evtSignal != NULL)
{
SetEvent(p_evtSignal);
Sleep(30);
CloseHandle(p_evtSignal);
}
lpKBD = (PKBDLLHOOKSTRUCT)lParam;
RETAILMSG(1,(TEXT("code %d,wParam 0x%x,vkCode %d,scanCode %d,flags 0x%08x,time %d\n"),code,wParam,lpKBD->vkCode,lpKBD->scanCode,lpKBD->flags,lpKBD->time));
switch(lpKBD->vkCode)
{
case LKeyUp:
if(wParam==0x101)
pIconBtn->AnswerTheCall();
break;
case LKeyCenter:
if(wParam==0x101)
pIconBtn->HangUpTheCall();
// if(wParam==0x101)
pIconBtn->CloseDialWindow();
break;
break;
case LKeyDown:
if(wParam==0x101)
{
pIconBtn->RunPilot();
}
break;
case RKeyUp:
if(wParam==0x100 && PowerOnOff==POWER_ON)
{
if(begin==0)
{
begin=1;
beginTime=lpKBD->time;
}
else
{
EndTime=lpKBD->time;
if(EndTime>beginTime)
{
if((EndTime-beginTime)>500)
{
pIconBtn->OpenDialWindowOnly();
}
if((EndTime-beginTime)>1000)
{
begin=0;
// pIconBtn->GoIntoSleep();
//pIconBtn->OpenDialWindow();
HANDLE hPwrButtonIntrEvent;
hPwrButtonIntrEvent = CreateEvent(NULL, FALSE, FALSE, TEXT("PowerOffEvt"));
if(hPwrButtonIntrEvent != NULL)
{
SetEvent(hPwrButtonIntrEvent);
Sleep(30);
CloseHandle(hPwrButtonIntrEvent);
//PowerOnOff=POWER_OFF;
}
}
}
}
}
/*else if(wParam==0x100 && PowerOnOff==POWER_OFF)
{
if(begin==0)
{
begin=1;
beginTime=lpKBD->time;
}
else
{
EndTime=lpKBD->time;
if(EndTime>beginTime)
{
if((EndTime-beginTime)>500)
{
begin=0;
// pIconBtn->GoIntoSleep();
HANDLE hPwrButtonIntrEventOff;
hPwrButtonIntrEventOff = CreateEvent(NULL, FALSE, FALSE, TEXT("PowerOnEvt"));
if(hPwrButtonIntrEventOff != NULL)
{
SetEvent(hPwrButtonIntrEventOff);
CloseHandle(hPwrButtonIntrEventOff);
PowerOnOff=POWER_ON;
}
}
}
}
}*/
else
{
begin=0;
}
break;
case RKeyCenter:
if(wParam==0x101)
pIconBtn->ExchangeFrame();
break;
case RKeyDown:
if(wParam==0x101)
pIconBtn->OpenDialWindow();
break;
default:
break;
}
// delete pIconBtn;
return (LRESULT)0;//ProcessMessageFilter(code, (LPMSG)lParam);
}
void SetKBHook()
{
ActivateKBHook(NULL, (HOOKPROC)hwKeyBdHook);
}
WINCEKBHOOK_API BOOL ActivateKBHook(HINSTANCE hInstance, HOOKPROC LLKeyboardHookCallbackFunction)
{
//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, LLKeyboardHookCallbackFunction, 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;
}
/**
* Function Name:DeactivateKBHook
*
* Function Desc:Uninstall the KB hook
*
* Parameters:
* none
* Returns:
* true if we exit gracefully
*
* Author: Prathamesh Kulkarni
**/
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;
}
详情
回复
发表于 2007-8-20 15:40
| ||
|
||
此帖出自嵌入式系统论坛
| ||
|
||
此帖出自嵌入式系统论坛
| ||
|
||
此帖出自嵌入式系统论坛
| ||
|
||
EEWorld Datasheet 技术支持