端钩键盘 [英] end hooking key board

查看:77
本文介绍了端钩键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码

Hi i have this code

#include <QApplication>
#include <QMainWindow>
#include <QTime>
#include <QChar>
#include <iostream>
#include <Windows.h>
#include <QDebug>
#pragma comment(lib, "user32.lib")
using namespace std;
HHOOK hHook=NULL;

void UpdateKeyState(BYTE *keystate,int keycode)
{
	keystate[keycode]=GetKeyState(keycode);
}


LRESULT CALLBACK MyLowLevelKeyBoardProc(int nCode,WPARAM wParam,LPARAM lParam)
{
	KBDLLHOOKSTRUCT cKey=*((KBDLLHOOKSTRUCT *)lParam);
	wchar_t buffer[5];
	BYTE keyboard_state[256];
	GetKeyboardState(keyboard_state);
	UpdateKeyState(keyboard_state,VK_SHIFT);
	UpdateKeyState(keyboard_state,VK_CAPITAL);
	UpdateKeyState(keyboard_state,VK_CONTROL);
	UpdateKeyState(keyboard_state,VK_MENU);
	HKL keyboard_layout=GetKeyboardLayout(0);
	char lpszName[0x100]={0};
	DWORD dwMsg=1;
	dwMsg +=cKey.scanCode<<16;
	dwMsg +=cKey.flags<<24;
	int i=GetKeyNameText(dwMsg,(LPTSTR)lpszName,255);
	int result = ToUnicodeEx(cKey.vkCode,cKey.scanCode,keyboard_state,buffer,4,0,keyboard_layout);
	buffer[4]=L'\0';
	HWND hwnd;
	POINT p;
	int xsave,ysave;
	GetCursorPos(&p);
	qDebug()<<"Key:"<<cKey.vkCode<<" "<<QString::fromUtf16((ushort*)buffer)<<" "<<QString::fromUtf16((ushort*)lpszName)<<" X:"<<p.x<<" Y:"<<p.y;

	return CallNextHookEx(hHook,nCode,wParam,VK_SPACE);
}
int main(int argc, char *argv[])
{
	QApplication a(argc, argv);
	QMainWindow w;
	w.setWindowTitle("2Key");
	w.show();
	hHook = SetWindowsHookEx(WH_KEYBOARD_LL,MyLowLevelKeyBoardProc,NULL,0);
	int as;
	return a.exec();
}


它有一些qt,但是还不行,我需要结束钩子函数,我想从MyLowLevelKeyBoardProc函数中退出,但是我无法从中退出
我想脱钩,过一会儿再钩


it has some qt to but that doesn''t mater i need to end the hook function i want to come out from the MyLowLevelKeyBoardProc func but i cant How i can get out from that
i want unhook and after a while hook again

推荐答案

OP澄清之后,我发现我原来的建议是适用的.

与临时取消挂钩事件处理程序不同,在每个应用程序生命周期中仅对其进行一次挂钩即可.在生命周期的中间,如果需要临时停用钩子活动,请保留一些状态标志并检查其值以查看该功能是启用还是禁用.由于此任务的多线程性质,您可能还需要以互锁的方式为该标志分配一个值.请参阅:
http://msdn.microsoft.com/zh-我们/library/windows/desktop/ms684122%28v=vs.85%29.aspx [
After the clarification from OP, I can see that my original suggestion is applicable.

Instead of un-hooking the event handler temporarily, hook and unhook it only once per application lifetime. In the middle of lifetime, if temporary deactivation of hooked activity is required, preserve some status flag and check its value to see if the functionality is enabled or disabled. You also may need to assign a value to the flag in the interlocked manner due to multi-threading nature of this task. Please see:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms684122%28v=vs.85%29.aspx[^].

static LONG enableCount = 0; 

//...

LRESULT CALLBACK MyLowLevelKeyBoardProc(int nCode,WPARAM wParam,LPARAM lParam) {
   if (enableCount < 1) return;
   //... do all your processing here
}

//...

void Enable() {
   InterlockedIncrement(&enableCount);
}
void Disable() {
   InterlockedDecrement(&enableCount);
}



这样,对EnableDisable的调用可以嵌套在任何线程中并执行.最好使用try-finally语句.使用RAII技术也很好.请参阅:
http://en.wikipedia.org/wiki/RAII [> http://stackoverflow.com/questions/7779652/try-catch- finally-construct-is-it-in-c11 [ ^ ].

—SA



This way, the calls to Enable and Disable could be nested and performed in any thread; which is the best to do with try-finally statement. It''s also good to use the RAII technique. Please see:
http://en.wikipedia.org/wiki/RAII[^],
http://stackoverflow.com/questions/7779652/try-catch-finally-construct-is-it-in-c11[^].

—SA


这篇关于端钩键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆