Click to See Complete Forum and Search --> : Catching all keyboard input in msg loop
Gyannea
December 23rd, 2004, 09:15 AM
I need to excute a certain function no matter what is going on. I want to trigger this event when the user presses the right and left shift keys and the space bar. So in the message loop I look for the WM_KEYDOWN message and then use the GetKeyState() function and the VK_SPACE parameter to identify the combination. In many cases it works, but in certain dialog boxes created within a class it does not.
I assume that the system somehow gets priority in this case and is trapping the keyboard input and handling it. How can I get all keyboard messages while in my application?
Thanks for any insight, explanations, and info which will increase my understanding of why this is happening.
Brian
NoHero
December 23rd, 2004, 10:07 AM
Another good way to catch keyboard inputs is, to use 'RegisterHotkey()' to assign a app-wide hotkey. This will also work if you are using dialog boxes.
I thought it would work if you write something like that:
MSG msg;
BOOL bRetVal = 0;
while ( (bRetVal = GetMessage(&msg, NULL, 0, 0)) != 0 )
{
if ( bRetVal == -1 )
{ // handle error somewhat
}
else
{
if ( msg.message == WM_KEYDOWN )
{
// Handle key down message somewhat else
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
Have you tried it? Does it work?
Marc G
December 23rd, 2004, 10:42 AM
Another good way to catch keyboard inputs is, to use 'RegisterHotkey()' to assign a app-wide hotkey. This will also work if you are using dialog boxes.RegisterHotkey registers a system-wide hotkey ;)
I thought it would work if you write something like that:
MSG msg;
BOOL bRetVal = 0;
while ( (bRetVal = GetMessage(&msg, NULL, 0, 0)) != 0 )
{
if ( bRetVal == -1 )
{ // handle error somewhat
}
else
{
if ( msg.message == WM_KEYDOWN )
{
// Handle key down message somewhat else
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
Have you tried it? Does it work?This won't work when the window shows a modal dialogbox, because all keyboard events will go to that dialogs dialogproc.
NoHero
December 23rd, 2004, 10:58 AM
Of course. If you create a modal dialogbox by using 'DialogBox' call the function won't return until the dialog box is closed. To avoid problems with this you should use 'CreateDialog()' only. If some small tricks - like resetting the focus to the window if it loses it to one of the parent windows - you can emulate a modal dialog box behaviour.
In that case you should use modeless dialog boxes only.
Another solution: Create a global accelerator table you use for every dialogbox. Define the hotkeys for the dialogbox there and bind them with the local IDC that will handle the input. The only thing you have to do know is to redirect the messages from the dialog to it's parent window. (The best solution I can think of the moment). But this will work for modeless dialogs only.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.