- 积分
- 45
贡献30
飞刀4 FD
注册时间2016-4-14
在线时间42 小时
扫一扫,手机访问本帖
|
以下只是本人最近的一些心得 ,摸索了一段时间,以供其他有同样需要的同学参考。
Te6410的6个按键驱动及其应用程序示例
本文所说都是基于te6410在wince下的操作,首先下载飞凌最新的bsp(本人对驱动不是很懂),加载6个按键的驱动(将驱动文件夹FLK拷到drivers目录下面),并更改几个地方,然后编译。
1、在platform.bib中添加
;------------- FLKEY Driver -------------------------------------------------------------
;IF BSP_NOFLKEY !
FLK.dll $(_FLATRELEASEDIR)\FLK.dll NK SHK
;ENDIF BSP_NOFLKEY !
2、在platform.reg中添加
;================ FLKEY ===========
[HKEY_LOCAL_MACHINE\Drivers\BuiltIn\FLK]
"Prefix"="FLK"
"Dll"="FLK.dll"
"Order"=dword:34
"Index"=dword:1
这里主要讲怎么在应用程序中使用按键,其实飞凌已经把按键驱动写成中断,嵌入到键盘按键消息里面去了。6个按键分别对应四个方向键和一个enter、一个esc键。我们只需要去捕捉相应的按键码就够了。我在这里用构建键盘钩子过程去捕捉按键消息,(关于钩子和消息可以查看相应的windows编程相关书籍)
以vs2005对话框程序为例
在头文件中定义如下:
全局变量定义
//定义钩子结构体
typedef struct
{
DWORD vkCode;
DWORD scanCode;
DWORD flags;
DWORD time;
ULONG_PTR dwExtraInfo;
} KBDLLHOOKSTRUCT, *PKBDLLHOOKSTRUCT;
//hook
typedef LRESULT (WINAPI *_CallNextHookEx)(HHOOK, int, WPARAM, LPARAM);
typedef LRESULT (WINAPI *_UnhookWindowsHookEx)(HHOOK);
typedef LRESULT (CALLBACK* HOOKPROC)(int code, WPARAM wParam, LPARAM lParam);
typedef HHOOK (WINAPI *_SetWindowsHookExW)(int, HOOKPROC, HINSTANCE, DWORD);
static _CallNextHookEx CallNextHookEx;
static _SetWindowsHookExW SetWindowsHookEx;
static _UnhookWindowsHookEx UnhookWindowsHookEx;
成员变量和函数
HINSTANCE g_hHookApiDLL;
HHOOK g_hInstalledLLKBDhook;
bool ActivateKBHook(HINSTANCE hInstance,HOOKPROC LLKeyboardHookCallbackFunction);
在源文件中实现如下
全局函数
LRESULT CALLBACK KeyboardProc(int nCode,WPARAM wParam, LPARAM lParam)
{
KBDLLHOOKSTRUCT *pkbhs = (KBDLLHOOKSTRUCT*) lParam;
if(wParam == WM_KEYUP)
{
//AfxMessageBox(L"key up /n");
switch(pkbhs->vkCode)
{
case 0x31:
AfxMessageBox(L"1 up /n");
break;
}
}
if(wParam == WM_KEYDOWN)
{
//AfxMessageBox(L"key down /n");
switch(pkbhs->vkCode)
{
case 38:
AfxMessageBox(L"S3 down /n");
//((CPv_PlatformDlg*)AfxGetMainWnd())->m_deviceIO.BeepOn();//蜂鸣器响
break;
case 40:
AfxMessageBox(L"S4 down /n");
//((CPv_PlatformDlg*)AfxGetMainWnd())->m_deviceIO.BeepOff();//蜂鸣器关
break;
case 37:
AfxMessageBox(L"S5 down /n");
break;
case 39:
AfxMessageBox(L"S6 down /n");
break;
}
}
// LRESULT RetVal = CallNextHookEx( NULL, nCode, wParam, lParam );
return CallNextHookEx(NULL,nCode,wParam,lParam);
}
成员函数
//激活键盘hook函数,其实就是加载钩子过程相关函数的dll
bool CPv_PlatformDlg::ActivateKBHook(HINSTANCE hInstance,HOOKPROC LLKeyboardHookCallbackFunction)
{
//RETAILMSG(DEBUG_CODE,(L"进入ActivateKBHook/r/n"));
//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
AfxMessageBox(L"载入coredll.dll失败/r/n");
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
AfxMessageBox(L"获取SetWindowsHookExW地址失败/r/n");
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)
{
//int ret;
//ret=GetLastError();
AfxMessageBox(L"SetWindowsHookEx失败!");
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)
{
AfxMessageBox(L"获取_CallNextHookEx地址失败!");
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)
{
AfxMessageBox(L"获取_UnhookWindowsHookEx地址失败!");
return false;
}
}
//all the APIs are loaded and the application is hooked
return true;
}
最后在初始化的地方启动钩子过程函数BOOL CPv_PlatformDlg::OnInitDialog()
ActivateKBHook(AfxGetInstanceHandle() ,KeyboardProc);
其中编译的时候会提示WH_KEYBOARD_LL这个宏定义找不到,自己重新定义一下。
#define WH_KEYBOARD_LL 20
备注:te6410上几个按键的键值
s3是方向键“上” 键值是38
s4是方向键“下” 键值是40
s5是方向键“左” 键值是37
s6是方向键“右” 键值是39
s7是方向键“esc” 键值是27
s8是方向键“enter” 键值是13
|
|