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. handling the RETURN key

handling the RETURN key

Scheduled Pinned Locked Moved C / C++ / MFC
help
8 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.
  • T Offline
    T Offline
    Tara14
    wrote on last edited by
    #1

    Hi, When I press the enter key in a dialog box, the default is CDialog::OnOk (I guess). What I usually do is catch the enter key message and make it a tab key. But for this particular project, I want OnButtonAdd function to be executed if the focus is on an edit box and the Return key is pressed. I tried this:

    BOOL CApplyValue::PreTranslateMessage(MSG* pMsg)
    {
    if (pMsg->message== WM_KEYDOWN && pMsg->wParam==13)
    {
    CEdit *e;
    e = (CEdit*)GetDlgItem(IDC_EDIT_All);
    if (e->GetFocus() == this)
    OnButtonAdd();
    pMsg->wParam=9;
    }
    return CDialog::PreTranslateMessage(pMsg);
    }

    No error come up with this, but it doesent work. So I must be doing it wrong. Please can you tell me the right way. Thanks, Tara_


    Fortitudine Vincimus!_

    L D 2 Replies Last reply
    0
    • T Tara14

      Hi, When I press the enter key in a dialog box, the default is CDialog::OnOk (I guess). What I usually do is catch the enter key message and make it a tab key. But for this particular project, I want OnButtonAdd function to be executed if the focus is on an edit box and the Return key is pressed. I tried this:

      BOOL CApplyValue::PreTranslateMessage(MSG* pMsg)
      {
      if (pMsg->message== WM_KEYDOWN && pMsg->wParam==13)
      {
      CEdit *e;
      e = (CEdit*)GetDlgItem(IDC_EDIT_All);
      if (e->GetFocus() == this)
      OnButtonAdd();
      pMsg->wParam=9;
      }
      return CDialog::PreTranslateMessage(pMsg);
      }

      No error come up with this, but it doesent work. So I must be doing it wrong. Please can you tell me the right way. Thanks, Tara_


      Fortitudine Vincimus!_

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      If the CEdit has the focus, then the Dialog never recieves the WM_KEYDOWN message. The message is being processed by the CEdit. You need to derive a class from CEdit and use your PreTranslateMessage code there.

      1 Reply Last reply
      0
      • T Tara14

        Hi, When I press the enter key in a dialog box, the default is CDialog::OnOk (I guess). What I usually do is catch the enter key message and make it a tab key. But for this particular project, I want OnButtonAdd function to be executed if the focus is on an edit box and the Return key is pressed. I tried this:

        BOOL CApplyValue::PreTranslateMessage(MSG* pMsg)
        {
        if (pMsg->message== WM_KEYDOWN && pMsg->wParam==13)
        {
        CEdit *e;
        e = (CEdit*)GetDlgItem(IDC_EDIT_All);
        if (e->GetFocus() == this)
        OnButtonAdd();
        pMsg->wParam=9;
        }
        return CDialog::PreTranslateMessage(pMsg);
        }

        No error come up with this, but it doesent work. So I must be doing it wrong. Please can you tell me the right way. Thanks, Tara_


        Fortitudine Vincimus!_

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

        When the edit control receives focus, use CDialog::SetDefID() on the Add button.


        "Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

        "Judge not by the eye but by the heart." - Native American Proverb

        T 1 Reply Last reply
        0
        • D David Crow

          When the edit control receives focus, use CDialog::SetDefID() on the Add button.


          "Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

          "Judge not by the eye but by the heart." - Native American Proverb

          T Offline
          T Offline
          Tara14
          wrote on last edited by
          #4

          CDialog::SetDefID() Thats a wonderful thing I have learnt today. Thanks a lot._


          Fortitudine Vincimus! -- modified at 10:52 Tuesday 18th July, 2006_

          D 1 Reply Last reply
          0
          • T Tara14

            CDialog::SetDefID() Thats a wonderful thing I have learnt today. Thanks a lot._


            Fortitudine Vincimus! -- modified at 10:52 Tuesday 18th July, 2006_

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

            Tara14 wrote:

            Thats a wonderful thing...

            Only if it works, though. Let us know.


            "Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

            "Judge not by the eye but by the heart." - Native American Proverb

            T 1 Reply Last reply
            0
            • D David Crow

              Tara14 wrote:

              Thats a wonderful thing...

              Only if it works, though. Let us know.


              "Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

              "Judge not by the eye but by the heart." - Native American Proverb

              T Offline
              T Offline
              Tara14
              wrote on last edited by
              #6

              Yes!! It did. It worked. Because of this I also realised one more thing. In the settings for a button in a dialog box resource, there is an option 'Default button'. If you select that option for any button, when the enter key is pressed that particular button is activated. Earlier, I had thought that the option 'Default button' had to do with its appearence! Thanks. Tara_


              Fortitudine Vincimus!_

              S 1 Reply Last reply
              0
              • T Tara14

                Yes!! It did. It worked. Because of this I also realised one more thing. In the settings for a button in a dialog box resource, there is an option 'Default button'. If you select that option for any button, when the enter key is pressed that particular button is activated. Earlier, I had thought that the option 'Default button' had to do with its appearence! Thanks. Tara_


                Fortitudine Vincimus!_

                S Offline
                S Offline
                stanlymt
                wrote on last edited by
                #7

                This is not the proper way of handling RETURN Key. If you have more than one edit box and if you want to handle RETURN in different ways for each edit box, then this method fails. Check the below code: BOOL CApplyValue::PreTranslateMessage(MSG* pMsg) { if (pMsg->message== WM_KEYDOWN && pMsg->wParam == VK_RETURN) { if (pMsg->hwnd == ::GetDlgItem(*this, IDC_EDIT1) //Replace with proper resource ID { //Do your processing for Edit box1 GetDlgItem(*this, IDC_EDIT2)->SetFocus(); //If you want to set focus to next edit box } else if(pMsg->hwnd == ::GetDlgItem(*this, IDC_EDIT2) { //Do your processing for Edit box2 } return TRUE; } return CDialog::PreTranslateMessage(pMsg); }

                T 1 Reply Last reply
                0
                • S stanlymt

                  This is not the proper way of handling RETURN Key. If you have more than one edit box and if you want to handle RETURN in different ways for each edit box, then this method fails. Check the below code: BOOL CApplyValue::PreTranslateMessage(MSG* pMsg) { if (pMsg->message== WM_KEYDOWN && pMsg->wParam == VK_RETURN) { if (pMsg->hwnd == ::GetDlgItem(*this, IDC_EDIT1) //Replace with proper resource ID { //Do your processing for Edit box1 GetDlgItem(*this, IDC_EDIT2)->SetFocus(); //If you want to set focus to next edit box } else if(pMsg->hwnd == ::GetDlgItem(*this, IDC_EDIT2) { //Do your processing for Edit box2 } return TRUE; } return CDialog::PreTranslateMessage(pMsg); }

                  T Offline
                  T Offline
                  Tara14
                  wrote on last edited by
                  #8

                  Thanks a lot._


                  Fortitudine Vincimus!_

                  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