67194:67194 > 玩儿转嵌入式开发
[导读]//?已经写过一个?CE5.0?下的键盘钩子:http://blog.csdn.net/91program/article/details/1961570 //?前一段时间在坛子看到有人在问?CE6.

//?已经写过一个?CE5.0?下的键盘钩子:http://blog.csdn.net/91program/article/details/1961570
//?前一段时间在坛子看到有人在问?CE6.0?下钩子的实现,有的说不行,有的说行,最后也没有结果
//?今天试了下,是可以的,包括鼠标的钩子。不过速度上好像是有些问题,待有时间再看看吧。?
//?看代码吧。

//?KeyBoardHook.h?

#ifdef?KEYBOARDHOOK_EXPORTS
#define?KEYBOARDHOOK_API?__declspec(dllexport)
#else
#define?KEYBOARDHOOK_API?__declspec(dllimport)
#endif

//?This?class?is?exported?from?the?KeyBoardHook.dll
class?KEYBOARDHOOK_API?CKeyBoardHook
{
public:
??CKeyBoardHook(void);
??//?TODO:?add?your?methods?here.
};

extern?"C"?KEYBOARDHOOK_API?void?InstallKeyBoardHook(void);
extern?"C"?KEYBOARDHOOK_API?void?UnKeyBoardHook(void);
extern?"C"?KEYBOARDHOOK_API?LRESULT?CALLBACK?KeyBoardProc(int?nCode,?WPARAM?wParam,?LPARAM?lParam);


//?KeyBoardHook.cpp?
//?KeyBoardHook.cpp?:?Defines?the?entry?point?for?the?DLL?application.
//

#include?"stdafx.h"
#include?"KeyBoardHook.h"
#include//?告诉编译器将变量放入它自己的数据共享节中
#pragma?data_seg("KeyHookData")

HINSTANCE?ghInst?=?NULL;
HHOOK?ghKeyHook?=?NULL;

#pragma?data_seg()

//?告诉编译器设置共享节的访问方式为:读,写,共享
#pragma?comment(linker,?"/SECTION:KeyHookData,RWS")

BOOL?APIENTRY?DllMain(HANDLE?hModule,DWORD??ul_reason_for_call,LPVOID?lpReserved)
{
??switch?(ul_reason_for_call)
??{
??case?DLL_PROCESS_ATTACH:
????ghInst?=?(HINSTANCE)hModule;
????break;
??case?DLL_THREAD_ATTACH:
??case?DLL_THREAD_DETACH:
??case?DLL_PROCESS_DETACH:
????break;
??}
??return?TRUE;
}

//?This?is?the?constructor?of?a?class?that?has?been?exported.
//?see?KeyBoardHook.h?for?the?class?definition
CKeyBoardHook::CKeyBoardHook()
{?
??return;?
}

extern?"C"?KEYBOARDHOOK_API?void?InstallKeyBoardHook(void)
{
??RETAILMSG(1,(L"[Keyboard?HOOK]Install?HOOK:?0x%xrn",ghInst));
??if(ghInst)
??{
????ghKeyHook?=?SetWindowsHookEx(WH_KEYBOARD_LL,?KeyBoardProc,?ghInst,?0);
????if(NULL?==?ghKeyHook)
????{
??????RETAILMSG(1,(L"[Keyboard?HOOK]HOOK?create?failed:?%drn",GetLastError()));
????}
??}
}

extern?"C"?KEYBOARDHOOK_API?void?UnKeyBoardHook(void)
{
??RETAILMSG(1,(L"[Keyboard?HOOK]Uninstall?HOOK:0x%xrn",ghKeyHook));
??if(ghKeyHook)
??{
????UnhookWindowsHookEx(ghKeyHook);
????ghKeyHook?=?NULL;
??}

??ghInst?=?NULL;
}

//?CE6?的键盘?HOOK?的响应速度慢,?原因不明!
//?CallNextHookEx(hKeyHook,?nCode,?wParam,?lParam);?//继续传递消息
//?SIP?上仅?Ctl/Shift/CAP/Esc/Backspace?可以处理到
//?key?value?17/?16??/?20/?27/?8
extern?"C"?KEYBOARDHOOK_API?LRESULT?CALLBACK?KeyBoardProc(int?nCode,?WPARAM?wParam,?LPARAM?lParam)
{
#if?_DEBUG
??RETAILMSG(1,(L"[Keyboard?HOOK]Proc,Code?is:?%d,Down/Up?is:?%srn",nCode,256?==?wParam???L"Down"?:?L"Up"));
#endif

??if(HC_ACTION?!=?nCode)
??{
????return?CallNextHookEx(ghKeyHook,?nCode,?wParam,?lParam);
??}

??PKBDLLHOOKSTRUCT?pKBHookStruct?=?(PKBDLLHOOKSTRUCT)lParam;
??RETAILMSG(1,(L"[Keyboard?HOOK]vkCode?value?is:?%drn",pKBHookStruct->vkCode));
??RETAILMSG(1,(L"[Keyboard?HOOK]scanCode?value?is:?%drn",pKBHookStruct->scanCode));
??RETAILMSG(1,(L"[Keyboard?HOOK]dwExtraInfo?value?is:?%drn",pKBHookStruct->dwExtraInfo));

??if(WM_KEYDOWN?==?wParam)
??{
????//?响应按键声,并限制需要向上?Post?的?vkCode
????switch(pKBHookStruct->vkCode)
????{
????case?VK_UP:
????case?VK_DOWN:
????case?VK_LEFT:
????case?VK_RIGHT:
??????break;
????default:
??????break;
????}
??}
??else?if?(WM_KEYUP?==?wParam)
??{
????//?限制需要向上Post的vkCode
????switch?(pKBHookStruct->vkCode)
????{
????case?VK_UP:
????case?VK_DOWN:
????case?VK_LEFT:
????case?VK_RIGHT:
??????break;
????default:
??????break;
????}
??}

??//?若注释此句,?则被?HOOK?到的按键不再发到?CE?系统?
??return?CallNextHookEx(ghKeyHook,?nCode,?wParam,?lParam);??//?继续传递消息
}


//?调用示例

//?Keyboard?hook?test
typedef?void?(*pInstallKeyboardHook)(void);
typedef?void?(*pUnKeyboardHook)(void);
HINSTANCE?ghKeyboardModule?=?NULL;
pInstallKeyboardHook??InKeyBoardHook?=?NULL;
pUnKeyboardHook??UnKeyBoardHook?=?NULL;


ghKeyboardModule?=?LoadLibrary(L"\AppStore\SystemInfo\KeyBoardHook.dll");
if(ghKeyboardModule)
{
??InKeyBoardHook?=?(pInstallKeyboardHook)GetProcAddress(ghKeyboardModule,?L"InstallKeyBoardHook");
??UnKeyBoardHook?=?(pUnKeyboardHook)GetProcAddress(ghKeyboardModule,?L"UnKeyBoardHook");
}
if(NULL?!=?InKeyBoardHook)
{
??InKeyBoardHook();
}


玩儿转嵌入式开发

327 篇文章

关注

发布文章

技术子站

更多

论坛活动

  • 新年历,ADI精品视频课堂开始啦
    新年历,ADI精品视频课堂开始啦
  • 【看视频赢好礼】贸泽电子视频课程大讲堂
  • RT-Thread应用创新设计大赛,报名即领开发板~
  • 21ic下载站“资源奖励计划”升级!再发3450元~
更多

资料下载

  • (网盘)刘凯老师STM32视频教程及配套资源
  • (网盘)电路精讲班视频
  • 龙芯2k lite 开发板教育派硬件规格
  • (网盘)电路强化班视频
  • Beken BK7252 麻雀一号开发板
  • 最全Altium designer封装库700MB(含3D模型),网盘分享
  • (网盘)quartus基本语法等
  • (网盘)stm32四轴飞行器资料
  • 雅特力 AT-START-F403A开发板
  • 非接触式电子体温计(额温枪)PCB+程序源码
XML 地图 | Sitemap 地图