全局钩子接收键盘消息并产生新的全局键盘消息

下面代码的功能是接收了键盘的空格后,产生键盘事件一个Esc按键的KeyDown,一个空格的KeyDown,在一个Esc按键的KeyDown消息。是按顺序产生这些事件的,代码如下:

#include <Windows.h>
#define DllExport _declspec(dllexport)
HHOOK g_KeyBoard = NULL;
BOOL static Test = FALSE;
LRESULT CALLBACK KeyBoardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if(VK_SPACE ==wParam && (1 == (lParam >> 30 & 1)) &&Test)
{
if(Test)
Test = FALSE;
else
Test = TRUE;
return 1;
}
if(VK_SPACE == wParam &&(0 == (lParam >> 30 & 1)))
{

/*下面五行代码是用来判断我电脑的一个特定程序的窗口的*/
HWND CurrentWnd = GetActiveWindow();
CHAR szClassName[255];
GetClassName(CurrentWnd,szClassName,255);
if(strlen(szClassName) < 100)
return CallNextHookEx(g_KeyBoard,nCode,wParam,lParam);
keybd_event(VK_ESCAPE,0,0,60);
keybd_event(VK_SPACE,0,0,60);
keybd_event(VK_ESCAPE,0,0,60);
//keybd_event(VK_SPACE,0,KEYEVENTF_KEYUP,60);
return 1;
}
return CallNextHookEx(g_KeyBoard,nCode,wParam,lParam);

}

//导出函数,注册全局钩子
DllExport void SetHook()
{
g_KeyBoard = SetWindowsHookEx(WH_KEYBOARD,KeyBoardProc,
GetModuleHandle(TEXT("PlusEclipse")),0);
}

你可能感兴趣的:(键盘)