Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. A dialog with button controls does not have focus OnKeyDown

A dialog with button controls does not have focus OnKeyDown

Scheduled Pinned Locked Moved C / C++ / MFC
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AglaiaMasaki
    wrote on last edited by
    #1

    Hello. At the first time, I made a dialog with OnKeyDown function and there was no control on the dialog. My OnKeyDown function worked well. I pressed a key(spacebar) and OnKeyDown function was called. :) Next, I made some CButton controls on the dialog. And I soon noticed something wrong, OnKeyDown had no effect. I found the reason. One of the button controls had focus automatically, and when I pressed spacebar that button was clicked. :omg: (If there was no control on the dialog, that action would call OnKeyDown function.) So I added the code "AfxGetApp()->m_pMainWnd->SetFocus();" in OnInitDialog, yet that seems ineffective. I want to make the dialog grab 'keydown' focus like the first time, not the button control. I need ideas and advice. Thank you for reading this.

    May the sky bring you a full measure of health and prosperity.

    C S 2 Replies Last reply
    0
    • A AglaiaMasaki

      Hello. At the first time, I made a dialog with OnKeyDown function and there was no control on the dialog. My OnKeyDown function worked well. I pressed a key(spacebar) and OnKeyDown function was called. :) Next, I made some CButton controls on the dialog. And I soon noticed something wrong, OnKeyDown had no effect. I found the reason. One of the button controls had focus automatically, and when I pressed spacebar that button was clicked. :omg: (If there was no control on the dialog, that action would call OnKeyDown function.) So I added the code "AfxGetApp()->m_pMainWnd->SetFocus();" in OnInitDialog, yet that seems ineffective. I want to make the dialog grab 'keydown' focus like the first time, not the button control. I need ideas and advice. Thank you for reading this.

      May the sky bring you a full measure of health and prosperity.

      C Offline
      C Offline
      Code o mat
      wrote on last edited by
      #2

      If you read the documentation of CDialog::OnInitDialog[^], you find this:

      Return Value Specifies whether the application has set the input focus to one of the controls in the dialog box. If OnInitDialog returns nonzero, Windows sets the input focus to the first control in the dialog box. The application can return 0 only if it has explicitly set the input focus to one of the controls in the dialog box.

      So return FALSE from OnInitDialog. Does that help?

      > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Life: great graphics, but the gameplay sux. <

      A 1 Reply Last reply
      0
      • C Code o mat

        If you read the documentation of CDialog::OnInitDialog[^], you find this:

        Return Value Specifies whether the application has set the input focus to one of the controls in the dialog box. If OnInitDialog returns nonzero, Windows sets the input focus to the first control in the dialog box. The application can return 0 only if it has explicitly set the input focus to one of the controls in the dialog box.

        So return FALSE from OnInitDialog. Does that help?

        > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Life: great graphics, but the gameplay sux. <

        A Offline
        A Offline
        AglaiaMasaki
        wrote on last edited by
        #3

        I tried and the button control seemed losing its 'focus', but when I pressed spacebar, the button was still clicked. (The problem is that my action(pressing the key) didn't call the OnKeyDown function because of the buttons. Once I removed the buttons for test, the program worked well. Yet I have to add buttons.) Anyway, thanks for your answer. If you have any ideas, please let me know. :)

        May the sky bring you a full measure of health and prosperity.

        C 1 Reply Last reply
        0
        • A AglaiaMasaki

          I tried and the button control seemed losing its 'focus', but when I pressed spacebar, the button was still clicked. (The problem is that my action(pressing the key) didn't call the OnKeyDown function because of the buttons. Once I removed the buttons for test, the program worked well. Yet I have to add buttons.) Anyway, thanks for your answer. If you have any ideas, please let me know. :)

          May the sky bring you a full measure of health and prosperity.

          C Offline
          C Offline
          Code o mat
          wrote on last edited by
          #4

          I'm not sure but you could try catching the key press in the PreTranslateMessage[^] method of the CDialog...

          > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Life: great graphics, but the gameplay sux. <

          A 1 Reply Last reply
          0
          • C Code o mat

            I'm not sure but you could try catching the key press in the PreTranslateMessage[^] method of the CDialog...

            > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Life: great graphics, but the gameplay sux. <

            A Offline
            A Offline
            AglaiaMasaki
            wrote on last edited by
            #5

            Your advice greatly helped me. I think you were busy, nevertheless, you helped me. Thank you very much. :) I am using the PreTranslateMessage method like this :

            #define SPACEBAR 32
            ...
            BOOL C~~Dlg::PreTranslateMessage(MSG* pMsg)
            {
            if(pMsg->message == WM_KEYDOWN) {
            if(pMsg->wParam == SPACEBAR)
            OnKeyDown(SPACEBAR, 1, 57);

            And when I press down spacebar, my OnKeyDown function is called. :-D Yet there remains a little problem that the button is still being clicked. I am trying to find the solution now. If I find good ideas, I will post it on this post. Have a nice day! :)

            May the sky bring you a full measure of health and prosperity.

            C 1 Reply Last reply
            0
            • A AglaiaMasaki

              Your advice greatly helped me. I think you were busy, nevertheless, you helped me. Thank you very much. :) I am using the PreTranslateMessage method like this :

              #define SPACEBAR 32
              ...
              BOOL C~~Dlg::PreTranslateMessage(MSG* pMsg)
              {
              if(pMsg->message == WM_KEYDOWN) {
              if(pMsg->wParam == SPACEBAR)
              OnKeyDown(SPACEBAR, 1, 57);

              And when I press down spacebar, my OnKeyDown function is called. :-D Yet there remains a little problem that the button is still being clicked. I am trying to find the solution now. If I find good ideas, I will post it on this post. Have a nice day! :)

              May the sky bring you a full measure of health and prosperity.

              C Offline
              C Offline
              Code o mat
              wrote on last edited by
              #6

              Yourwelcome. :) Try returning TRUE from PreTranslateMessage after you called your OnKeyDown handler, this should stop the button from handling the keyhit too and get pressed. Good luck. :)

              > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Life: great graphics, but the gameplay sux. <

              1 Reply Last reply
              0
              • A AglaiaMasaki

                Hello. At the first time, I made a dialog with OnKeyDown function and there was no control on the dialog. My OnKeyDown function worked well. I pressed a key(spacebar) and OnKeyDown function was called. :) Next, I made some CButton controls on the dialog. And I soon noticed something wrong, OnKeyDown had no effect. I found the reason. One of the button controls had focus automatically, and when I pressed spacebar that button was clicked. :omg: (If there was no control on the dialog, that action would call OnKeyDown function.) So I added the code "AfxGetApp()->m_pMainWnd->SetFocus();" in OnInitDialog, yet that seems ineffective. I want to make the dialog grab 'keydown' focus like the first time, not the button control. I need ideas and advice. Thank you for reading this.

                May the sky bring you a full measure of health and prosperity.

                S Offline
                S Offline
                Sophiya Chen
                wrote on last edited by
                #7

                hi,get the key message use PreTranslateMessage() function. you can try..

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups