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 problem when you press ESC in a CEdit ctrl

A problem when you press ESC in a CEdit ctrl

Scheduled Pinned Locked Moved C / C++ / MFC
c++helptutorialquestion
4 Posts 2 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.
  • Z Offline
    Z Offline
    zengkun100
    wrote on last edited by
    #1

    Everyone knows that when you press the 'Enter' key or 'ESC' key in a dialog application, even if in a CEdit ctrl that has focus, the dialog will exit. If you want to prevent this unwantable annoying default behavior, you should write some code. There is an atricle about how to catch those Enter keys in controls at CP. The author tell us to add a hanled to catch WM_GETDLGCODE message,if the function's return code is DLGC_WANTALLKEYS, then our controls will know every message and we have the right to decide what to do to those 'Enter','ESC' keys. So I subclass a CEdit class called CMyEdit, and added two message handlers OnGetDlgCode and OnKeyDown. In CMyEdit's OnKeyDown function, I can catch VK_RETURN and VK_ESCAPE, and the dialog will not exit even if Enter or ESC is pressed in the edit ctrl. Hooray! ;P But!!! when I change this CMyEdit from singleline to multiline, everything changed! Now OnKeyDown function will not see VK_ESCAPE any more, but VK_RETURN is still catched. And when the ESC is pressed in the ctrl, dialog exit. :mad:Why DLGC_WANTALLKEYS doesn't work now? After I googled for half a day. I found a solution--override CEdit's PreTranslateMessage and catch all key's there. I want to known why DLGC_WANTALLKEYS works well for single line CEdit but not for multiline? And which is the best way to prevent dialog from exiting when the ESC or Enter is pressed in a CEdit ctrl? Thank you all! :)

    A Chinese VC++ programmer

    M 1 Reply Last reply
    0
    • Z zengkun100

      Everyone knows that when you press the 'Enter' key or 'ESC' key in a dialog application, even if in a CEdit ctrl that has focus, the dialog will exit. If you want to prevent this unwantable annoying default behavior, you should write some code. There is an atricle about how to catch those Enter keys in controls at CP. The author tell us to add a hanled to catch WM_GETDLGCODE message,if the function's return code is DLGC_WANTALLKEYS, then our controls will know every message and we have the right to decide what to do to those 'Enter','ESC' keys. So I subclass a CEdit class called CMyEdit, and added two message handlers OnGetDlgCode and OnKeyDown. In CMyEdit's OnKeyDown function, I can catch VK_RETURN and VK_ESCAPE, and the dialog will not exit even if Enter or ESC is pressed in the edit ctrl. Hooray! ;P But!!! when I change this CMyEdit from singleline to multiline, everything changed! Now OnKeyDown function will not see VK_ESCAPE any more, but VK_RETURN is still catched. And when the ESC is pressed in the ctrl, dialog exit. :mad:Why DLGC_WANTALLKEYS doesn't work now? After I googled for half a day. I found a solution--override CEdit's PreTranslateMessage and catch all key's there. I want to known why DLGC_WANTALLKEYS works well for single line CEdit but not for multiline? And which is the best way to prevent dialog from exiting when the ESC or Enter is pressed in a CEdit ctrl? Thank you all! :)

      A Chinese VC++ programmer

      M Offline
      M Offline
      Maxwell Chen
      wrote on last edited by
      #2

      The simplest solution, working well, and you won't need to override anything of CEdit class:

      void CTestDlg::OnBtnClickedCancel()
      { // [Cancel] key handler.
      CWnd* pWnd = GetFocus();
      if(pWnd == dynamic_cast<CWnd*>(&m_ctlEditMultiline)) {
      // When the focus is in the multi-line edit control.
      return; // Ignore.
      }
      OnCancel(); // Exit dialog box.
      }

      Maxwell Chen

      Z 1 Reply Last reply
      0
      • M Maxwell Chen

        The simplest solution, working well, and you won't need to override anything of CEdit class:

        void CTestDlg::OnBtnClickedCancel()
        { // [Cancel] key handler.
        CWnd* pWnd = GetFocus();
        if(pWnd == dynamic_cast<CWnd*>(&m_ctlEditMultiline)) {
        // When the focus is in the multi-line edit control.
        return; // Ignore.
        }
        OnCancel(); // Exit dialog box.
        }

        Maxwell Chen

        Z Offline
        Z Offline
        zengkun100
        wrote on last edited by
        #3

        Yes, If you handle the ESC key in the upmost dialogbox, the there will be many if there. If the focus is in a edit box then blablala, and If the focus is in a listctrl then something else. This is why I appreciate handling the WM_GETDLGCODE message. Then each control has its own way to handle key message. I believe the code will be cleaner. :) But it doesn't works well for multiline CEdit ctrls. Any way, Thank you Maxwell Chen :)

        A Chinese VC++ programmer

        M 1 Reply Last reply
        0
        • Z zengkun100

          Yes, If you handle the ESC key in the upmost dialogbox, the there will be many if there. If the focus is in a edit box then blablala, and If the focus is in a listctrl then something else. This is why I appreciate handling the WM_GETDLGCODE message. Then each control has its own way to handle key message. I believe the code will be cleaner. :) But it doesn't works well for multiline CEdit ctrls. Any way, Thank you Maxwell Chen :)

          A Chinese VC++ programmer

          M Offline
          M Offline
          Maxwell Chen
          wrote on last edited by
          #4

          ;P

          Maxwell Chen

          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