孙鑫VC++深入详解笔记
dwThreadId为当前线程ID时为进程内钩子。当为0时为全局钩子与运行的所有进程都相关,此时lpfn为一个DLL中的钩子过程,hMod为DLL句柄
HHOOK SetWindowsHookEx( int idHook, // hook type HOOKPROC lpfn, // hook procedure HINSTANCE hMod, // handle to application instance DWORD dwThreadId // thread identifier );
LRESULT CallNextHookEx( HHOOK hhk, // handle to current hook int nCode, // hook code passed to hook procedure WPARAM wParam, // value passed to hook procedure LPARAM lParam // value passed to hook procedure );
BOOL UnhookWindowsHookEx( HHOOK hhk // handle to hook procedure );
LRESULT CALLBACK MouseProc( int nCode, // hook code WPARAM wParam, // message identifier LPARAM lParam // mouse coordinates );LRESULT CALLBACK KeyboardProc( int code, // hook code WPARAM wParam, // virtual-key code LPARAM lParam // keystroke-message information );进程内钩子:1,定义钩子函数:LRESULT CALLBACK KeyboardProc( int code, // hook code WPARAM wParam, // virtual-key code LPARAM lParam // keystroke-message information ){
//二种按键判断VK_SPACE==wParam 或 HIBYTE(GetKeyState(VK_SPACE))
// if(VK_SPACE==wParam){ // return 1; // } if(HIBYTE(GetKeyState(VK_SPACE))){ return TRUE; }
//alt组合键 (lParam>>29&1)值为1时为ALT为按下状态
// if(VK_F4==wParam&&(lParam>>29&1)){ // return TRUE; // }
//组合键 下列为CTRL+O // if(HIBYTE(GetKeyState(VK_CONTROL))&&HIBYTE(GetKeyState(_T('O')))){ // return 1; // }
return CallNextHookEx(g_hKeyboard,code,wParam,lParam); }
2,定义 HHOOK g_hKeyboard=NULL;3,加载钩子 g_hKeyboard=SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,NULL,GetCurrentThreadId());以下方法可以在钩子的回调函数中区分出大小写,很简单: BYTE ks[256]; GetKeyboardState(ks); WORD w; UINT scan; scan=0; ToAscii(wParam,scan,ks,&w,0); char ch =char(w); ch就是区分过大小写后的输入了,试试,很简单。