Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
Hi i have this code
C++
#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();
}

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
Posted
Updated 6-Sep-12 7:07am
v3
Comments
[no name] 6-Sep-12 11:31am    
If I understood you correctly, you need to call UnhookWindowsHookEx
ZiDoM 6-Sep-12 13:07pm    
yes buy in this case i cant because i want unhook and after a while hook again
Sergey Alexandrovich Kryukov 6-Sep-12 14:04pm    
Not clear what prevents you from unhooking and hooking again. But I would suggest to keep the hook at all times, but process it differently depending on some status. This would be much saver and simpler technique.

Please explain the purpose of all that if you want more specific advice.
--SA
ZiDoM 6-Sep-12 14:11pm    
I want to create hotkey but the hotkey should be active sometimes in the middle of the program i want to deactivate and active another time again

1 solution

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[^].

C++
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);
}


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
 
Share this answer
 
Comments
ZiDoM 6-Sep-12 16:23pm    
I need to come out of the MyLowLevelKeyBoardProc in this way i cant for example i need to go to main function then come back MyLowLevelKeyBoardProc func
Sergey Alexandrovich Kryukov 6-Sep-12 17:32pm    
Either I don't understand what do you mean or you are missing something. What is "come out"? Why go to main function (and what is it)? Why?
--SA
ZiDoM 6-Sep-12 17:49pm    
its hard to explain the exact thing but i mean come out from MyLowLevelKeyBoardProc function and continue in int main()
Sergey Alexandrovich Kryukov 7-Sep-12 13:44pm    
I'm pretty much sure my approach helps in all cases. Did you understand it? and it's value? You just set aside delicate aspects of installation and uninstallation of a hook...
--SA
Malli_S 7-Sep-12 7:36am    
You are not STUCK in the hook function. The hook function is getting called whenever any keyboard event occurs. You are inside the QApplication's message loop. Check your code line :

return a.exec();

What you are asking that can't be achieved unless exiting the application. Better you specify your purpose of doing that, and ask for some other solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900