首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 嵌入开发 > WinCE >

在mobile下怎么截取pocket Pc硬件按钮的按键消息

2012-02-17 
在mobile下如何截取pocket Pc硬件按钮的按键消息?在默认的情况下,按下PPC的硬件按键会启动其它的应用程序(

在mobile下如何截取pocket Pc硬件按钮的按键消息?
在默认的情况下,按下PPC的硬件按键会启动其它的应用程序(如:联系人、日历等)。我想在我的应用程序中处理PPC的硬件按键事件,应该如何做呢?


[解决办法]
获得虚拟键值,
可以通过WM_KEYDOWN 消息来获得,
如下说明:
WM_KEYDOWN nVirtKey = (int) wParam;
lKeyData = lParam;


通过RegisterHotKey注册热键,
通过这个WM_HOTKEY消息来截取对应虚拟键消息并做相应处理
[解决办法]
你在网上查查相关的键盘钩子的资料.这个应该能完成你要的功能.这是我以前做的项目用到的,你先看一下
#include "stdafx.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
HINSTANCE hInst=NULL;//引用实例的名柄

BOOL TimeInterval(LPSYSTEMTIME OldTime,LPSYSTEMTIME NowTime,int Interval);
/** 
*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
**/
BOOL ActivateKBHook(HINSTANCE hInstance)
{
//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;
hInst=hInstance;
//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, KeyboardProc, 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;
}


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;
}

//----------------------------------
// SetVideoPower - Turns on or off the display
//设定显示屏开关
int SetVideoPower (BOOL fOn) {
VIDEO_POWER_MANAGEMENT vpm;
int rc, fQueryEsc;
HDC hdc;
// Get the display dc.
hdc = GetDC (NULL);
// See if supported.
fQueryEsc = SETPOWERMANAGEMENT;
rc = ExtEscape (hdc, QUERYESCSUPPORT, sizeof (fQueryEsc), 
(LPSTR)&fQueryEsc, 0, 0); //查询系统是否支持关闭屏幕
if (rc == 0) {
// No support, fail.
ReleaseDC (NULL, hdc);
return -1;
}
// Fill in the power management structure.
vpm.Length = sizeof (vpm);
vpm.DPMSVersion = 1;
if (fOn==false) 
vpm.PowerState = VideoPowerOn;
else
vpm.PowerState = VideoPowerOff;
// Tell the driver to turn on or off the display.
rc = ExtEscape (hdc, SETPOWERMANAGEMENT, sizeof (vpm), 
(LPSTR)&vpm, 0, 0);
// Always release what you get.
ReleaseDC (NULL, hdc);
return 0;
}


LRESULT CALLBACK KeyboardProc(int nCode,WPARAM wParam,LPARAM lParam)
{
staticDWORD KeyValues=0;//报警键的键值
staticboolFirstValue=true;//0为假,非0为真
static BOOL Lock=false;//true锁屏,False打开
static LPSYSTEMTIME timeDown=new SYSTEMTIME();;//键按下时间
static LPSYSTEMTIME timeUp=new SYSTEMTIME();//键抬起时间

if(((KBDLLHOOKSTRUCT*)lParam)->vkCode ==134)
{
//Generate the keyboard press event of the mapped key
if(wParam==256&&true==FirstValue)
{
GetSystemTime(timeDown);
FirstValue=false;
}
if(wParam==257)
{
GetSystemTime(timeUp);
FirstValue=true;
if(TimeInterval(timeDown,timeUp,2))
{
Lock=!Lock;
if(Lock==true)//启动应用程序
{
BOOL result1=CreateProcess(TEXT("\\Program Files\\SmartWin32_Alarm\\SmartWin32_Hook.exe"),TEXT(""), NULL, NULL,FALSE, 0, NULL, NULL, NULL, NULL);
}
}
}
}
if(Lock==false)
{
SetVideoPower (Lock); 
return CallNextHookEx(g_hInstalledLLKBDhook, nCode,wParam, lParam);
}
else
{
SetVideoPower (Lock); 
//GPRSConnect *conn=new GPRSConnect();
//conn->Connnection();
//conn->StartThread();
return 10;
}
return 10;
}



BOOL TimeInterval(LPSYSTEMTIME OldTime,LPSYSTEMTIME NowTime,int Interval)
{
if(NowTime->wHour==OldTime->wHour)
{
if(NowTime->wMinute==OldTime->wMinute)//分钟相同
{
if(NowTime->wSecond-OldTime->wSecond>Interval)
{
return true;
}
}
else
{
return true;
}
}
return false;
}

热点排行