-
win32 gui
Hi,
I have made a button, when pressed program should activate some shortcuts. I am using the GetAsyncKeyState function to find out what button is pressed. But where should I put this, currently I have put it this way, but when activated the GUI stops responding untill the shortcut button has pressed:
Code:
BOOL CALLBACK DialogProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDC_ACTIVATE:
while(1) { while(!GetAsyncKeyState(vKey)) Sleep(50);
/* Do Stuff.. */
break;
}
break;
}
break;
return 0;
}
Any info about how I can fix this is welcome,
apsync
[edit]I am using Windows XP as OS and VC++6 as compiler[/edit]
-
You can't just halt the windows message loop by throwing in a while(1) and a sleep.
Everything has to be, get in, do some small amount of work based on the current message and get out again.
So for instance
Code:
BOOL CALLBACK DialogProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static bActivated = FALSE; // a bit of persistent memory for this function
switch (message)
{
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDC_ACTIVATE:
bActivated = TRUE;
break;
}
break;
case WM_TIMER: // maybe WM_KEYDOWN, WM_CHAR, etc etc - RTM to find suitable ones
if ( bActivated && GetAsyncKeyState(vKey) ) {
/* Do Stuff.. */
}
break;
}
return 0;
}
Obviously, in response to some other message, you set the flag to FALSE again.
-
If you want your application to respond to the key even if it's not the current app, perhaps RegisterHotKey() is what you're looking for.
-
Thanks for the replies. I see what you mean Salem, I have a totally different view now, thanks.
Also, Cactus_Hugger, I will check out what RegisterHotKey function does and if it is any good in my situation, but for now I will stick with Salem's solution.
-
>>// maybe WM_KEYDOWN, WM_CHAR, etc etc - RTM to find suitable ones
Can you provide a link, been searching on msdn but couldnt find it. Isn't 'message identifiers' the right keyword for it?
-
Since you're using VC6, just point at a message identifier and press F1
Or point at a message identifier, right click and press "goto definition".
That will take you to the windows header file containing a whole bunch of them, where you can then F1 all the ones which might interest you.
-