CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2007
    Location
    France
    Posts
    329

    WM_KEYDOWN and focus

    If I click on a button then it will keep the focus and I won't get any WM_KEYDOWN messages any more as long as I dont click outside and then back on the window.

    What is the cause of this behaviour and how to avoid it?

    The button:
    Code:
        switch (message)
        {
        case WM_CREATE:
            CreateWindowExA(WS_EX_CLIENTEDGE,"BUTTON", "Play",
                WS_CHILD|WS_VISIBLE,
                20, 20, 80, 25, hWnd,
                (HMENU)IDB_PLAY, 0, NULL);
    Last edited by MasterDucky; July 12th, 2014 at 04:50 AM.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: WM_KEYDOWN and focus

    Quote Originally Posted by MasterDucky View Post
    If I click on a button then it will keep the focus and I won't get any WM_KEYDOWN messages any more as long as I dont click outside and then back on the window.

    What is the cause of this behaviour?
    It is "by design". Example: you "click" some button (press but release it yet!), but then you understand that this button-click would be wrong. So you move cursor from this button and after that release the button. In such a case no button-click event will be generated.
    Quote Originally Posted by MasterDucky View Post
    I... how to avoid it?
    What for?
    Victor Nijegorodov

  3. #3
    Join Date
    Dec 2007
    Location
    France
    Posts
    329

    Re: WM_KEYDOWN and focus

    Yes but I need to push that button to start the application. After that on the other hand I want to handle WM_KEYDOWN messages.

    But since the focus stays on the button I dont get the WM_KEYDOWN messages.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: WM_KEYDOWN and focus

    In the code that handles the button click have you tried changing the focus?
    See http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Dec 2007
    Location
    France
    Posts
    329

    Re: WM_KEYDOWN and focus

    It worked. Thank you.

    First I put it in the case that called the play() in the menu not on the button that's why it didn't work.

  6. #6
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: WM_KEYDOWN and focus

    Quote Originally Posted by MasterDucky View Post
    Yes but I need to push that button to start the application. After that on the other hand I want to handle WM_KEYDOWN messages.

    But since the focus stays on the button I dont get the WM_KEYDOWN messages.
    Forcible focus displacing is a sort of inelegant solution as for me.

    There are several standard mechanisms in Windows to process keyboard input transparently and independent of current focus location: 1) translating accelerators, 2) hooking message loop and 3) hooking keyboard input at low level. All those ones have their merits and demerits, depending on the ultimate goals to be achieved.
    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured