Windows编程之键盘鼠标钩子

以一个简单的钩子程序为例,屏蔽所有键盘鼠标消息,按下F1停止钩子

#include
HHOOK hook,hook2;
LRESULT CALLBACK kbdProc(int code, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdShow,int iCmdLine)
{
	HINSTANCE Hinstance;
	Hinstance = GetModuleHandle(TEXT("user32.dll"));
	if (Hinstance == NULL)
	{
		MessageBox(NULL, TEXT("没找到啊!"), TEXT("没找到"), 0);
	}
	hook=SetWindowsHookEx(WH_KEYBOARD_LL, kbdProc, Hinstance, NULL);
	hook2 = SetWindowsHookEx(WH_MOUSE_LL, kbdProc, Hinstance, NULL);
	MSG msg;
	while (GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	UnhookWindowsHookEx(hook);
	UnhookWindowsHookEx(hook2);
	return (int)msg.wParam;
}
LRESULT CALLBACK kbdProc(int code, WPARAM wParam, LPARAM lParam)
{
	if (code != HC_ACTION)
	{
		return 1;
	}
	if (wParam == WM_LBUTTONDOWN || wParam == WM_MOUSEMOVE || wParam == WM_RBUTTONDOWN || wParam == WM_RBUTTONUP || wParam == WM_LBUTTONUP)
	{
		return 1;
	}
	PKBDLLHOOKSTRUCT param = (PKBDLLHOOKSTRUCT)lParam;
	if (param->vkCode == VK_F1)
	{
		PostQuitMessage(0);
	}
	else
	{
		return 1;
	}
	return CallNextHookEx(hook, code, wParam, lParam);
}

代码不多说,以后再完善。

你可能感兴趣的:(Windows编程学习记录)