Reputation: 47
In my interface I have a few buttons, a combo box and a window where I display sequence of images. Those buttons navigate through the sequence, but I also wanted to navigate through the LEFT and RIGHT arrows on the keyboard. After compilation the keyboard keys work fine, because Focus is set at the main window, but after clicking a button or combo the keyboard keys doesn't work. I manage it with SetFocus(main_hwnd) in my main loop but then the combo box doesn't react when clicked.
Example below:
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
CreateWindowEx(0, "COMBOBOX", "", WS_CHILD | WS_VISIBLE | CBS_SORT | CBS_DROPDOWNLIST, 478, 5, 50, 200, hwnd, (HMENU)ID_COMBO1, GetModuleHandle(NULL), NULL);
break;
case WM_COMMAND:
break;
case WM_KEYDOWN:
switch(wParam)
{
case VK_SPACE:
PostQuitMessage(0);
break;
}
break;
case WM_DESTROY:
PostQuitMessage (0);
break;
default:
return DefWindowProc (hwnd, message, wParam, lParam);
}
// SetFocus(hwnd);
return 0;
}
assumig this example: after compilation the SPACE KEY will work, but after clicking combobox, it won't work. If I use SetFocus(hwnd) SPACE KEY will always work but the combo box will be disabled.
Thanks for help.
Upvotes: 0
Views: 863
Reputation: 2661
Yeah, your window procedure is for that window only, a combo box is a separate window. I like to handle my hotkeys inside the message loop like so:
MSG msg;
while (GetMessage(&msg, NULL, 0, 0) > 0)
{
if (msg.message == WM_KEYDOWN)
{
if ((UINT)msg.wParam == VK_SPACE)
{
return 0; // or use postquitmessage
}
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
Upvotes: 1
Reputation: 941227
They keyboard message is dispatched to the window with the focus. That will not be your main window, the combobox sees it. This is normally handled by the dialog box logic built into Windows but you probably didn't create a dialog. Not sure how far you want to go with this, any class library handles this for you, primarily by sub-classing the controls and looking for navigation keys in the message loop, before dispatching the message.
Upvotes: 1