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. Enter and Esc

Enter and Esc

Scheduled Pinned Locked Moved C / C++ / MFC
question
6 Posts 4 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
    Alexander M
    wrote on last edited by
    #1

    Hi there, I have a dialog box (derived from CDialog), which contains an edit control. I want to handle the esc and enter keys at the message handler of the edit control, but the esc and enter messages are being translated by accelerators and the whole dialog box closes. How can I make the keys handled by the edit control? Thanks for advice, Alex Don't try it, just do it! ;-)

    D M G 3 Replies Last reply
    0
    • A Alexander M

      Hi there, I have a dialog box (derived from CDialog), which contains an edit control. I want to handle the esc and enter keys at the message handler of the edit control, but the esc and enter messages are being translated by accelerators and the whole dialog box closes. How can I make the keys handled by the edit control? Thanks for advice, Alex Don't try it, just do it! ;-)

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Alexander M. wrote: ...but the esc and enter messages are being translated by accelerators and the whole dialog box closes. See here.


      "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

      1 Reply Last reply
      0
      • A Alexander M

        Hi there, I have a dialog box (derived from CDialog), which contains an edit control. I want to handle the esc and enter keys at the message handler of the edit control, but the esc and enter messages are being translated by accelerators and the whole dialog box closes. How can I make the keys handled by the edit control? Thanks for advice, Alex Don't try it, just do it! ;-)

        M Offline
        M Offline
        myth7676
        wrote on last edited by
        #3

        the best way is to derive a class from CEdit and handle WM_CHAR. there you can do whatever you want in response to VK_RETURN, VK_ESCAPE. also make sure that in your dialog you have handlers for OK and Cancel. In those handlers comment the part with the call of the base class: void CBDlg::OnOK() { // TODO: Add extra validation here //CDialog::OnOK(); } Now your dialog will not exit when you press enter. same for OnCancel.. PS: Stay away from PreTranslate... as it's design is not for this kind of use.

        A 1 Reply Last reply
        0
        • M myth7676

          the best way is to derive a class from CEdit and handle WM_CHAR. there you can do whatever you want in response to VK_RETURN, VK_ESCAPE. also make sure that in your dialog you have handlers for OK and Cancel. In those handlers comment the part with the call of the base class: void CBDlg::OnOK() { // TODO: Add extra validation here //CDialog::OnOK(); } Now your dialog will not exit when you press enter. same for OnCancel.. PS: Stay away from PreTranslate... as it's design is not for this kind of use.

          A Offline
          A Offline
          Alexander M
          wrote on last edited by
          #4

          Yeah the dialog does not exit... but the CEdit does not receive a WM_CHAR message... I've already tried this, but without success. I will check PreTranslateMessage... lets see what is possible... Don't try it, just do it! ;-)

          1 Reply Last reply
          0
          • A Alexander M

            Hi there, I have a dialog box (derived from CDialog), which contains an edit control. I want to handle the esc and enter keys at the message handler of the edit control, but the esc and enter messages are being translated by accelerators and the whole dialog box closes. How can I make the keys handled by the edit control? Thanks for advice, Alex Don't try it, just do it! ;-)

            G Offline
            G Offline
            GKarRacer
            wrote on last edited by
            #5

            What you need to do is handle the WM_ONGETDLGCODE message for your edit control class. This message tells the dialog box procedure what keys to process for your control and which to skip. For example:

            UINT CMyEdit:OnGetDlgCode()
            {
            return( CEdit::OnGetDlgCode() | DLGC_WANTALLKEYS );
            }

            Then process in OnChar as normal.

            A 1 Reply Last reply
            0
            • G GKarRacer

              What you need to do is handle the WM_ONGETDLGCODE message for your edit control class. This message tells the dialog box procedure what keys to process for your control and which to skip. For example:

              UINT CMyEdit:OnGetDlgCode()
              {
              return( CEdit::OnGetDlgCode() | DLGC_WANTALLKEYS );
              }

              Then process in OnChar as normal.

              A Offline
              A Offline
              Alexander M
              wrote on last edited by
              #6

              Perfect answer, that was exactly what I wanted to know... thank you very much!!! Don't try it, just do it! ;-)

              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