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. OnKeyDown and OnPreTranslateMsg() problem...

OnKeyDown and OnPreTranslateMsg() problem...

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
13 Posts 5 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.
  • E Offline
    E Offline
    eli15021979
    wrote on last edited by
    #1

    Hi, I'm facing a problem which i don't know how to solve... I created a dialog which contains several edit boxes. When activating the dialog , the first edit box automatically receives the input focus. My problem is that I need to receive the ON_WM_KEYDOWN in the parent dialog, but since the focus is on the first edit box - I will never get it. I tried to catch the ON_WM_KEYDOWN message in PreTranslateMsg , but I'm facing the same problem. Anyone??? With best regards, Eli

    P R M M 4 Replies Last reply
    0
    • E eli15021979

      Hi, I'm facing a problem which i don't know how to solve... I created a dialog which contains several edit boxes. When activating the dialog , the first edit box automatically receives the input focus. My problem is that I need to receive the ON_WM_KEYDOWN in the parent dialog, but since the focus is on the first edit box - I will never get it. I tried to catch the ON_WM_KEYDOWN message in PreTranslateMsg , but I'm facing the same problem. Anyone??? With best regards, Eli

      P Offline
      P Offline
      Perspx
      wrote on last edited by
      #2

      If you want to monitor when the text in the edit box changes, you could do something with the EN_CHANGE notification which is sent when the text in an edit box changes.. If you want to monitor keyboard messages, then i'm not sure that the WM_KEYDOWN message is applicable to dialog boxes.. You could try creating a window using the CreateWindow() or CreateWindowEx API, then programatically creating the controls, and then testing for the WM_KEYDOWN message.. Hope this helps! --PerspX

      E 1 Reply Last reply
      0
      • P Perspx

        If you want to monitor when the text in the edit box changes, you could do something with the EN_CHANGE notification which is sent when the text in an edit box changes.. If you want to monitor keyboard messages, then i'm not sure that the WM_KEYDOWN message is applicable to dialog boxes.. You could try creating a window using the CreateWindow() or CreateWindowEx API, then programatically creating the controls, and then testing for the WM_KEYDOWN message.. Hope this helps! --PerspX

        E Offline
        E Offline
        eli15021979
        wrote on last edited by
        #3

        Hi PerspX , First,thanks for your quick reply. What is the difference between creating the control during the design time and during runtime(the control is being created in a different way???). Anyway,My problem is not catching the messages in the control, but catching the messages in the dialog... Thanks again, Eli

        P 1 Reply Last reply
        0
        • E eli15021979

          Hi, I'm facing a problem which i don't know how to solve... I created a dialog which contains several edit boxes. When activating the dialog , the first edit box automatically receives the input focus. My problem is that I need to receive the ON_WM_KEYDOWN in the parent dialog, but since the focus is on the first edit box - I will never get it. I tried to catch the ON_WM_KEYDOWN message in PreTranslateMsg , but I'm facing the same problem. Anyone??? With best regards, Eli

          R Offline
          R Offline
          rp_suman
          wrote on last edited by
          #4

          eli15021979 wrote:

          I need to receive the ON_WM_KEYDOWN in the parent dialog, but since the focus is on the first edit box - I will never get it.

          May this link help you setting focus to dialog Best Regards, Suman

          E 1 Reply Last reply
          0
          • E eli15021979

            Hi PerspX , First,thanks for your quick reply. What is the difference between creating the control during the design time and during runtime(the control is being created in a different way???). Anyway,My problem is not catching the messages in the control, but catching the messages in the dialog... Thanks again, Eli

            P Offline
            P Offline
            Perspx
            wrote on last edited by
            #5

            No i mean creating the window using the CreateWindow() or CreateWindowEx() API as opposed to creating a dialog window. Creating a window like this will create a "proper" window as opposed to a dialog window - take a look at this article for more information. I think - but im not sure - that some window messages dont get passed to dialog windows and i think WM_KEYDOWN is one of them.. I think I have tried that in the past to no avail.. So create the window "properly", then create the controls in it using CreateWindow() and CreateWindowEx(), and specifying the control classes (see this article on MSDN if you do not know these classes). Hope this helps! --PerspX

            1 Reply Last reply
            0
            • R rp_suman

              eli15021979 wrote:

              I need to receive the ON_WM_KEYDOWN in the parent dialog, but since the focus is on the first edit box - I will never get it.

              May this link help you setting focus to dialog Best Regards, Suman

              E Offline
              E Offline
              eli15021979
              wrote on last edited by
              #6

              Hi Suman, Thanks for the link... However,it doesn't say how to set the focus to the dialog itself,but only to one of the controls in the dialog(or maybe I didn't saw it?) Thanks again, Eli

              R 1 Reply Last reply
              0
              • E eli15021979

                Hi Suman, Thanks for the link... However,it doesn't say how to set the focus to the dialog itself,but only to one of the controls in the dialog(or maybe I didn't saw it?) Thanks again, Eli

                R Offline
                R Offline
                rp_suman
                wrote on last edited by
                #7

                Hi Eli, I am very sorry, there is no information about how to set focus to dialog itself in the URL I provided . May you can try posting the same question again. I hope somebody will know answer and help you!! Best Regards, Suman

                1 Reply Last reply
                0
                • E eli15021979

                  Hi, I'm facing a problem which i don't know how to solve... I created a dialog which contains several edit boxes. When activating the dialog , the first edit box automatically receives the input focus. My problem is that I need to receive the ON_WM_KEYDOWN in the parent dialog, but since the focus is on the first edit box - I will never get it. I tried to catch the ON_WM_KEYDOWN message in PreTranslateMsg , but I'm facing the same problem. Anyone??? With best regards, Eli

                  M Offline
                  M Offline
                  Mark Salsbery
                  wrote on last edited by
                  #8

                  Try returning FALSE from your dialog's OnInitDialog() override. Mark

                  "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

                  R 1 Reply Last reply
                  0
                  • M Mark Salsbery

                    Try returning FALSE from your dialog's OnInitDialog() override. Mark

                    "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

                    R Offline
                    R Offline
                    rp_suman
                    wrote on last edited by
                    #9

                    Mark Salsbery wrote:

                    Try returning FALSE from your dialog's OnInitDialog() override.

                    Dear Mark, I created test application and tried returning FALSE, but the focus is still in edit box. There should be some way to do this. Best Regards, Suman

                    M 1 Reply Last reply
                    0
                    • R rp_suman

                      Mark Salsbery wrote:

                      Try returning FALSE from your dialog's OnInitDialog() override.

                      Dear Mark, I created test application and tried returning FALSE, but the focus is still in edit box. There should be some way to do this. Best Regards, Suman

                      M Offline
                      M Offline
                      Mark Salsbery
                      wrote on last edited by
                      #10

                      hmmm Yes I tested that and I couldn't get it to work. The dialog wants the keyboard focus on a control no matter what. Trying to remove the focus was fruitless as well. I tried both SetFocus() and WM_NEXTDLGCTL. *trying some more stuff...* This looks promising ... add a ON_WM_SETFOCUS() entry to the dialog's message map Add an empty OnSetFocus() handler method: void CMYDlg::OnSetFocus(CWnd*) { } At the end of the dialog's OnInitDialog() override (instead of returning TRUE), add: SetFocus(); return FALSE; See if that works for you... Mark

                      "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

                      R 1 Reply Last reply
                      0
                      • E eli15021979

                        Hi, I'm facing a problem which i don't know how to solve... I created a dialog which contains several edit boxes. When activating the dialog , the first edit box automatically receives the input focus. My problem is that I need to receive the ON_WM_KEYDOWN in the parent dialog, but since the focus is on the first edit box - I will never get it. I tried to catch the ON_WM_KEYDOWN message in PreTranslateMsg , but I'm facing the same problem. Anyone??? With best regards, Eli

                        M Offline
                        M Offline
                        MANISH RASTOGI
                        wrote on last edited by
                        #11

                        You can only trace queued message in PreTranslateMessage. Manish Rastogi

                        1 Reply Last reply
                        0
                        • M Mark Salsbery

                          hmmm Yes I tested that and I couldn't get it to work. The dialog wants the keyboard focus on a control no matter what. Trying to remove the focus was fruitless as well. I tried both SetFocus() and WM_NEXTDLGCTL. *trying some more stuff...* This looks promising ... add a ON_WM_SETFOCUS() entry to the dialog's message map Add an empty OnSetFocus() handler method: void CMYDlg::OnSetFocus(CWnd*) { } At the end of the dialog's OnInitDialog() override (instead of returning TRUE), add: SetFocus(); return FALSE; See if that works for you... Mark

                          "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

                          R Offline
                          R Offline
                          rp_suman
                          wrote on last edited by
                          #12

                          Hi Mark, Thanks, now there is no focus on controls in a dialog. What is happening if you are not calling SetFocus() in OnInitDialog()? I hope Eli can get the messages in dialog! Best Regards, Suman

                          M 1 Reply Last reply
                          0
                          • R rp_suman

                            Hi Mark, Thanks, now there is no focus on controls in a dialog. What is happening if you are not calling SetFocus() in OnInitDialog()? I hope Eli can get the messages in dialog! Best Regards, Suman

                            M Offline
                            M Offline
                            Mark Salsbery
                            wrote on last edited by
                            #13

                            rp_suman wrote:

                            What is happening if you are not calling SetFocus() in OnInitDialog()?

                            According to the docs, the default handler for WM_SETFOCUS is the one that sets the focus to the correct control (it tracks which control internally). WM_SETFOCUS is a notification so there should be no huge harm in not passing it to the default handler. It will effect the user interface - you may want to set the focus to a specific control at some point. Users expect dialogs to act a certain way. With this method, when the user switches to another app and then switches back, the focus will be set to the dialog but the default control for the dialog may/will be set as well. A solution should probably be a bit more robust and track which control should have focus when nevessary. My 2 cents, Mark

                            "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

                            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