windows低级键盘钩子的应用-捕捉键盘的操作

4124 人浏览 | 时间: 2016-05-08 21:17:08 | 作者: 阿郎

    对于windows键盘钩子的知识点,请自行百度。我在这里就不再说了。对于动态链接库的知识,直接在本站搜索就行,我写了很多的学习心得。直接上代码:

现在win32中新建一个动态链接库工程:

HHOOK hKeyHook=NULL;

LRESULT CALLBACK KeyHook(int code, WPARAM wParam, LPARAM lParam)
{
	wchar_t *strkey;	
	wchar_t *str;
	switch(wParam)
	{
	case '1':strkey=L"1";break;
	case '2':strkey = L"2"; break;
	case '3':strkey = L"3"; break;
	case '4':strkey = L"4"; break;
	case '5':strkey = L"5"; break;
	case '6':strkey = L"6"; break;
	case '7':strkey = L"7"; break;
	case '8':strkey = L"8"; break;
	case '9':strkey = L"9"; break;
	case '0':strkey = L"0"; break;
	case 'A':strkey = L"a"; break;
	case 'B':strkey = L"b"; break;
	case 'C':strkey = L"c"; break;
	case 'D':strkey = L"d"; break;
	case 'E':strkey = L"e"; break;
	case 'F':strkey = L"f"; break;
	case 'G':strkey = L"g"; break;
	case 'H':strkey = L"h"; break;
	case 'I':strkey = L"i"; break;
	case 'J':strkey = L"j"; break;
	case 'K':strkey = L"k"; break;
	case 'L':strkey = L"l"; break;
	case 'M':strkey = L"m"; break;
	case 'N':strkey = L"n"; break;
	case 'O':strkey = L"o"; break;
	case 'P':strkey = L"p"; break;
	case 'Q':strkey = L"q"; break;
	case 'R':strkey = L"r"; break;
	case 'S':strkey = L"s"; break;
	case 'T':strkey = L"t"; break;
	case 'U':strkey = L"u"; break;
	case 'V':strkey = L"v"; break;
	case 'W':strkey = L"w"; break;
	case 'X':strkey = L"x"; break;
	case 'Y':strkey = L"y"; break;
	case 'Z':strkey = L"z"; break;
	case VK_BACK:strkey = L"back"; break;
	case VK_MENU:strkey = L"alt"; break;
	case VK_F1:strkey = L"f1"; break;
	case VK_F2:strkey = L"f2"; break;
	case VK_F3:strkey = L"f3"; break;
	case VK_F4:strkey = L"f4"; break;
	case VK_F5:strkey = L"f5"; break;
	case VK_F6:strkey = L"f6"; break;
	case VK_F7:strkey=L"f7";break;
	case VK_F8:strkey = L"f8"; break;
	case VK_F9:strkey = L"f9"; break;
	case VK_F10:strkey = L"f10"; break;
	case VK_F11:strkey = L"f11"; break;
	case VK_F12:strkey = L"f12"; break;
	case VK_CAPITAL:strkey = L""; break;
	case VK_CONTROL:strkey = L"control"; break;
	case VK_DELETE:strkey = L"delete"; break;
	case VK_DOWN:strkey = L"down"; break;
	case VK_RETURN:strkey = L"enter"; break;
	case VK_ESCAPE:strkey = L"escape"; break;
	case VK_HOME:strkey = L"home"; break;
	case VK_INSERT:strkey = L"insert"; break;
	case VK_NEXT:strkey = L"pagedown"; break;
	case VK_PAUSE:strkey = L"pause"; break;
	case VK_PRIOR:strkey = L"pageup"; break;
	case VK_SNAPSHOT:strkey = L"printscreen"; break;
	case VK_SPACE:strkey = L"space"; break;
	case VK_TAB:strkey = L"tab"; break;
	case VK_SCROLL:strkey = L"scrolllock"; break;
	case VK_NUMPAD1:strkey = L"1"; break;
	case VK_NUMPAD2:strkey = L"2"; break;
	case VK_NUMPAD3:strkey = L"3"; break;
	case VK_NUMPAD4:strkey = L"4"; break;
	case VK_NUMPAD5:strkey = L"5"; break;
	case VK_NUMPAD6:strkey = L"6"; break;
	case VK_NUMPAD7:strkey = L"7"; break;
	case VK_NUMPAD8:strkey = L"8"; break;
	case VK_NUMPAD9:strkey = L"9"; break;
	case VK_NUMPAD0:strkey = L"0"; break;
	case VK_END:strkey = L"end"; break;
	case VK_LEFT:strkey = L"left"; break;
	case VK_UP:strkey = L"up"; break;
	case VK_RIGHT:strkey = L"right"; break;
	case VK_NUMLOCK:strkey = L"numlock"; break;
	case VK_ADD:strkey = L"+"; break;
	case VK_DIVIDE:strkey = L"/"; break;
	case VK_SUBTRACT:strkey = L"-"; break;
	case VK_MULTIPLY:strkey = L"*"; break;
	case VK_DECIMAL:strkey = L"."; break;
	}
	MessageBoxW(NULL,strkey,L"键盘按键",MB_OK);
	return 0;
}

extern "C" _declspec(dllexport) void SetHook()
{
	hKeyHook=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyHook,GetModuleHandle(L"HookDll1"),0);
}

extern "C" _declspec(dllexport) void StopHook()
{
	UnhookWindowsHookEx(hKeyHook);
}
然后,我们新建一个对话框程序:
        m_hMod=LoadLibrary(L"./HookDll1.dll");
	FunSetHook SetHook;
	FunStopHook StopHook;
	if (m_hMod)
	{
		SetHook=(FunSetHook)GetProcAddress(m_hMod,"SetHook");
		StopHook=(FunStopHook)GetProcAddress(m_hMod,"StopHook");

		if (SetHook)
		{
			SetHook();
		}
		if (StopHook)
		{
			StopHook();
		}
		FreeLibrary(m_hMod);
	}
上面的代码是在OnInitDialog函数中添加的。

对于实现,我就不添加了。你自己可以实现看看。

相关阅读