Click here to Skip to main content
12,554,442 members (84,523 online)
Rate this:
Please Sign up or sign in to vote.
See more: C++ Win32 Visual-Studio
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;
void UpdateKeyState(BYTE *keystate,int keycode)

LRESULT CALLBACK MyLowLevelKeyBoardProc(int nCode,WPARAM wParam,LPARAM lParam)
	wchar_t buffer[5];
	BYTE keyboard_state[256];
	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);
	HWND hwnd;
	int xsave,ysave;
	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;
	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 6-Sep-12 5:19am
Updated 6-Sep-12 7:07am
Wes Aday 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
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.
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

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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

static LONG enableCount = 0; 

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

void Enable() {
void Disable() {

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

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
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?
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()
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...
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.
Agree. Only please address to OP, not to me. You can do it by commenting some existing OP's comment.
Thank you.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.161021.1 | Last Updated 6 Sep 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100