VC++键盘钩子demo

VC++ 专栏收录该内容
108 篇文章 4 订阅
HHOOK keyHook = NULL;


//先声明函数,实现在下面
LRESULT CALLBACK keyProc(int nCode, WPARAM wParam, LPARAM lParam);

//初始化钩子,关闭键盘事件(除了ALT+CTRL+DELETE)
void initHook()
{
	keyHook = SetWindowsHookEx(WH_KEYBOARD_LL, keyProc, GetModuleHandle(NULL), 0);
}

//销毁钩子
void unHook()
{
	if (keyHook)
	{
		UnhookWindowsHookEx(keyHook);
	}
}


//钩子用的函数
LRESULT CALLBACK keyProc(int nCode, WPARAM wParam, LPARAM lParam)
{
	//在WH_KEYBOARD_LL模式下lParam 是指向KBDLLHOOKSTRUCT类型地址
	KBDLLHOOKSTRUCT *pkbhs = (KBDLLHOOKSTRUCT *)lParam;
	//如果nCode等于HC_ACTION则处理该消息,如果小于0,则钩子子程就必须将该消息传递给 CallNextHookEx
	if (nCode == HC_ACTION) 
	{
		if (pkbhs->vkCode == VK_ESCAPE)
		{
			CLogHelp::ITCLog("keyProc---->Esc");
			if (g_pthisDlg)
			{
				CLogHelp::ITCLog("Closescreen_FULL");
				g_pthisDlg->Closescreen_FULL();
			}
		}
		else if (pkbhs->vkCode == VK_ESCAPE && GetAsyncKeyState(VK_CONTROL) & 0x8000 && GetAsyncKeyState(VK_SHIFT) & 0x8000) {
			//qDebug() << "Ctrl+Shift+Esc";
		}
		else if (pkbhs->vkCode == VK_ESCAPE && GetAsyncKeyState(VK_CONTROL) & 0x8000) 
		{
			//qDebug() << "Ctrl+Esc";
		}
		else if (pkbhs->vkCode == VK_TAB && pkbhs->flags & LLKHF_ALTDOWN) 
		{
			CLogHelp::ITCLog("Alt+Tab ");
		}
		else if (pkbhs->vkCode == VK_ESCAPE && pkbhs->flags &LLKHF_ALTDOWN)
		{
			CLogHelp::ITCLog("Alt+Esc");
		}
	
		else if (pkbhs->vkCode == VK_LWIN || pkbhs->vkCode == VK_RWIN) 
		{
			CLogHelp::ITCLog("VK_LWIN vs VK_RWIN ");
		}
		else if (pkbhs->vkCode == VK_F4 && pkbhs->flags & LLKHF_ALTDOWN) 
		{
			CLogHelp::ITCLog("Alt+F4 ");
		}
		else if (pkbhs->vkCode == VK_F1 && pkbhs->flags &LLKHF_ALTDOWN) 
		{
			CLogHelp::ITCLog("Alt+F1 back room ");
			unHook();
		}
		else if (pkbhs->vkCode == VK_F1)
		{
			CLogHelp::ITCLog("F1 Key press ");
		}

		return 1;//返回1表示截取消息不再传递,返回0表示不作处理,消息继续传递
	}
	return CallNextHookEx(keyHook, nCode, wParam, lParam);
}

 

  • 27
    点赞
  • 27
    评论
  • 5
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 点我我会动 设计师:白松林 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值