1

I am trying to get a GUI running for a C++ application but I am having an issue with key press events. Basically, everything works fine, as long as I do not click on any buttons (the main window registers key events), but as soon as I click on a button, the main window loses focus and it no longer captures key events. This might be a stupid question, but I am very new to C++. This is some of the code I am using:

Creation of the main window:

hwnd = CreateWindowEx (
               0,                   /* Extended possibilites for variation */
               szClassName,         /* Classname */
               "Application Name",  /* Title Text */
               WS_OVERLAPPEDWINDOW, /* default window */
               CW_USEDEFAULT,       /* Windows decides the position */
               CW_USEDEFAULT,       /* where the window ends up on the screen */
               540,                 /* The programs width */
               250,                 /* and height in pixels */
               HWND_DESKTOP,        /* The window is a child-window to desktop */
               NULL,                /* No menu */
               hThisInstance,       /* Program Instance handler */
               NULL                 /* No Window Creation data */
           );

Creation of one of the buttons:

CreateWindow(TEXT("button"), TEXT("Start"),
                 WS_VISIBLE | WS_CHILD,
                 x, y, width, height,
                 hwnd, (HMENU) 6, NULL, NULL);

I have also noticed that whenever I click on a button, the WM_KILLFOCUS event is fired, which is why I think that this is a focus issue. I have also tried capturing the WM_KILLFOCUS event and then set the focus again with SetActiveWindow but that crashed my program.

Any help would be appreciated :)

4
  • Are the buttons part of the main window, and who gets focus then? Or are you creating a dialog with buttons, and when you press a button in the dialog, the dialog gets the focus?
    – PlasmaHH
    Dec 15, 2011 at 11:18
  • I just have one main window with buttons in it. I create the window and buttons in the code provided, I do not create any dialogs. I do not know who gets the focus, since each button is a window it seems, i am assuming that once I click on the button, the button gains focus.
    – npinti
    Dec 15, 2011 at 11:20
  • I can't understand this: (HMENU) 6 Why is it required? Dec 15, 2011 at 11:29
  • That will allow me to identify which button has been pressed in the WindowProcedure function. I tried passing in just 6 but that causes the following error: invalid conversion from 'int' to 'HMENU__'| error: initializing argument 10 of 'HWND__ CreateWindowExA(DWORD, const CHAR*, const CHAR*, DWORD, int, int, int, int, HWND__, HMENU__, HINSTANCE__, void)'|
    – npinti
    Dec 15, 2011 at 11:41

3 Answers 3

4

It turned out that I was using the wrong function (SetWindowActive). Assaf Levy's answer seemed to complex for me and I thought that there might be another way around this. I managed to find the SetFocus function which gives the focus to any given window by providing it it's handle.

To make it work, what I needed to do was to, once that the the necessary code was executed within the WM_COMMAND block, I called the SetFocus function with the handle of the main window. This gave focus back to the main window and allowed it to receive events.

Note, putting the SetFocus in the WM_KILLFOCUS block will cause the buttons and any other component in it to become unresponsive to events.

3
  • I'm happy you found a shorter path, less code = better code :)
    – Assaf Levy
    Dec 15, 2011 at 12:31
  • 1
    Note that forcing focus back to the frame means that you can't click a button twice by hitting the space bar twice. The more conventional way of doing this is to parallel the IsDialogMessage technique. Why do you need to keep yanking focus away from where the user put it? Dec 15, 2011 at 15:29
  • 1
    Because if the main window looses focus, I am no longer able to capture key press events from the main window.
    – npinti
    Dec 15, 2011 at 15:43
2

This is by design. The main window is a window, but so the button is a window, and only one can have focus at any given time. If you don't want the button to "steal" the focus, add an OnFocus handler (or intercept WM_SETFOCUS) and immediately return focus to the previous window (I believe it's in the WPARAM of WM_SETFOCUS).

An easy hack would be:

  1. hMyButton = CreateWindow("button", ...).
  2. Define a MyButtonProc(HWND, UINT, WPARAM, LPARAM) function.
  3. Call SetWindowLong(hMyButton, GWL_WNDPROC, (LONG)MyButtonProc). Save the value returned by this function in a g_OldButtonProc.
  4. Inside MyButtonProc() catch WM_SETFOCUS, and call SetFocus(hMyMainWindow). Always return CallWindowProc(h_OldButtonProc, hwnd, msg, ...) at the end of your MyButtonProc() function, unless the message was WM_SETFOCUS.

That will do the trick (tested).

2
  • Yes that is what I thought since basically the same function is used to create both the window and the button. Any further clarification would be appreciated :)
    – npinti
    Dec 15, 2011 at 11:44
  • I managed to find a way around it, being a noob in C++ I found your answer a little bit too complex for me. Thanks a lot for providing the insight though :)
    – npinti
    Dec 15, 2011 at 12:20
0

The first answer was partially accurate. Subclassing the button can get "rid" of the "problem"; however handling WM_SETFOCUS be it in parent window, or subclass procedure or BN_SETFOCUS will result in unresponsive UI if you take the focsus from the button.

What you should override in the subclass procedure is WM_LBUTTONUP. Since by the time you release the mouse button you have already clicked the windows button.

Note I think this is utter rubbish for a button to be stealing focus. There should be a style like BS_NO_STEAL_FOCUS, that prevents this. As it is very cumbersome when you want another window to be handling key presses or scrolling.

/** Procedure for subclass. 
      This procedure is called first for the widget/control.
      Unhandled or partially handled message can goes to
      original procedure by calling DefSubclassProc(...).
      buttonProcEx takes all four params of normal procedure, plus a
       param for user defined object. 
      Note this is what win32 should have done in the first place.
 */
LRESULT CALLBACK buttonProcEx(HWND hwnd,uint msg,WPARAM,LPARAM,DWORD_PTR)
{
    if(msg == WM_LBUTTONUP) 
    {
     setFocus(GetParent(hwnd));
     return 0; //do not allow default behaviour
    }
    else return DefSubclassProc(hwnd,msg,wparam,lparam);
}


//Call this after creating your button

SetWindowSubclass((HWND)button,buttonProcEx,0,NULL);

or

struct Content {...}content; //lifetime should be long enough
SetWindowSubclass((HWND)button,buttonProcEx,0,(DWORD_PTR)&content);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.