//?已經(jīng)寫過一個(gè)?CE5.0?下的鍵盤鉤子:http://blog.csdn.net/91program/article/details/1961570
//?前一段時(shí)間在壇子看到有人在問?CE6.0?下鉤子的實(shí)現(xiàn),有的說不行,有的說行,最后也沒有結(jié)果
//?今天試了下,是可以的,包括鼠標(biāo)的鉤子。不過速度上好像是有些問題,待有時(shí)間再看看吧。?
//?看代碼吧。
//?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//?告訴編譯器將變量放入它自己的數(shù)據(jù)共享節(jié)中
#pragma?data_seg("KeyHookData")
HINSTANCE?ghInst?=?NULL;
HHOOK?ghKeyHook?=?NULL;
#pragma?data_seg()
//?告訴編譯器設(shè)置共享節(jié)的訪問方式為:讀,寫,共享
#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?的響應(yīng)速度慢,?原因不明!
//?CallNextHookEx(hKeyHook,?nCode,?wParam,?lParam);?//繼續(xù)傳遞消息
//?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)
??{
????//?響應(yīng)按鍵聲,并限制需要向上?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?到的按鍵不再發(fā)到?CE?系統(tǒng)?
??return?CallNextHookEx(ghKeyHook,?nCode,?wParam,?lParam);??//?繼續(xù)傳遞消息
}
//?調(diào)用示例
//?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();
}