Click here to Skip to main content
Rate this: bad
good
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;
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 6-Sep-12 6:19am
ZiDoM292
Edited 6-Sep-12 8:07am
v3
Comments
Wes Aday at 6-Sep-12 11:31am
   
If I understood you correctly, you need to call UnhookWindowsHookEx
ZiDoM at 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 at 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 at 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
good
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:
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);
}
 
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
  Permalink  
Comments
ZiDoM at 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 at 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 at 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 at 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 at 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.
Sergey Alexandrovich Kryukov at 7-Sep-12 13:45pm
   
Agree. Only please address to OP, not to me. You can do it by commenting some existing OP's comment.
Thank you.
--SA

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

  Print Answers RSS
0 DamithSL 400
1 Maciej Los 217
2 OriginalGriff 213
3 BillWoodruff 130
4 Zoltán Zörgő 85
0 OriginalGriff 7,953
1 DamithSL 6,139
2 Sergey Alexandrovich Kryukov 5,449
3 Maciej Los 5,293
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web04 | 2.8.141223.1 | Last Updated 6 Sep 2012
Copyright © CodeProject, 1999-2014
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