Thread: How to avoid keyboard focus being stolen by child window?

  1. #1
    Citizen of Awesometown the_jackass's Avatar
    Join Date
    Oct 2014
    Location
    Awesometown
    Posts
    269

    How to avoid keyboard focus being stolen by child window?

    I made a toolbox window that just shows a grid of normal buttons. I'd initially made it a non-overlapped (= without title bar and resizing) sibling window with code in the WM_SIZE and WM_MOVE messages to force it into the top left corner of the main window but it caused issues with minimization so I've made it a non-overlapped child window.

    Now the problem I'm having is that whenever I press the control key it's permanently stealing keyboard focus from the main window and unselected the selected tool button. Do you know of any way to avoid a child window from getting keyboard messages?

    Also do you know of any way of detecting Ctlr+<alphabet> keypresses other than matching with any of the first 32 characters? (Ex: I look for with character 26 for detecting Ctrl+Z)

  2. #2
    Citizen of Awesometown the_jackass's Avatar
    Join Date
    Oct 2014
    Location
    Awesometown
    Posts
    269
    NB: This only happens after I've changed the initial selected button in toolbox. Before that all keyboard messages are going to the main window and pressing control also doesent affect toolbox then.

  3. #3
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    You could register a keyboard hook, and only respond to the keypresses when your windows are in focus.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by the_jackass View Post
    NB: This only happens after I've changed the initial selected button in toolbox.
    That will be associated with the toolbox receiving some event. In the handler for that event, send a WM_SETFOCUS to the parent window.

    If the toolbox has its own event handler (which is normally the case), that will amount to the toolbox giving focus back to the parent. You could also do it in the handler for the parent (as the default handler routes messages to children) in which case the parent effectively steals focus back from its child.
    Last edited by grumpy; 10-31-2014 at 03:38 PM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Citizen of Awesometown the_jackass's Avatar
    Join Date
    Oct 2014
    Location
    Awesometown
    Posts
    269
    Yarin, I've no idea what a keyboard hook is....but thanks still.

    grumpy, I unfortunately probably wasnt detecting BN_UNPUSHED messages as I tried adding a WM_SETFOCUS call to parent window from it's handler without any avail. But I've made it work now by adding an SetFocus(GetParent(hwnd)) call to the WM_COMMAND handler of the toolbox. Both issues are solved now. Thanks a ton to you too!

  6. #6
    Citizen of Awesometown the_jackass's Avatar
    Join Date
    Oct 2014
    Location
    Awesometown
    Posts
    269
    btw I tried replacing the SetFocus() call by SendMessage(GetParent(hwnd),WM_SETFOCUS,(WPARAM)hw nd,0); where hwnd is toolbox window handle. This doesent seem to work do you by any chance know the reason?

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    If you read the documentation for SetFocus() it does a bit more than sending a single WM_SETFOCUS event.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Citizen of Awesometown the_jackass's Avatar
    Join Date
    Oct 2014
    Location
    Awesometown
    Posts
    269
    Ahh yes so sorry. I added a WM_KILLFOCUS message before the WM_SETFOCUS and it works correctly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Set Keyboard Focus to a non child control
    By gaurav_13191 in forum Windows Programming
    Replies: 3
    Last Post: 11-06-2011, 01:57 PM
  2. Keyboard focus without activation?
    By kgen in forum Windows Programming
    Replies: 3
    Last Post: 01-07-2008, 02:25 PM
  3. Ensuring That Main Child Window Receives Focus
    By SMurf in forum Windows Programming
    Replies: 3
    Last Post: 08-28-2006, 05:32 AM
  4. Child window cant receive keyboard inputs
    By Raison in forum Windows Programming
    Replies: 2
    Last Post: 11-21-2004, 09:46 AM
  5. Keyboard Focus
    By Aidman in forum Windows Programming
    Replies: 8
    Last Post: 01-07-2003, 05:41 PM