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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CEdit and the Return key

CEdit and the Return key

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

    I have a single line edit control and a class derived from CEdit. Now I want that the CFormView class which contains the edit control gets informed if the user presses the Return key if the Edit control has the input focus. I've tried to react on the NM_RETURN notification in my form view but I don't get the event in the form view (must I use reflection in my edit class?). Then I tried to capture the VK_RETURN key in the OnChar method of my edit class, but the OnChar method seems not to get any VK_RETURN events. So what would be the best way to get my form view informed if the user presses the return key in my edit control? Thanks in advance.

    R R J 3 Replies Last reply
    0
    • C CodeBrain

      I have a single line edit control and a class derived from CEdit. Now I want that the CFormView class which contains the edit control gets informed if the user presses the Return key if the Edit control has the input focus. I've tried to react on the NM_RETURN notification in my form view but I don't get the event in the form view (must I use reflection in my edit class?). Then I tried to capture the VK_RETURN key in the OnChar method of my edit class, but the OnChar method seems not to get any VK_RETURN events. So what would be the best way to get my form view informed if the user presses the return key in my edit control? Thanks in advance.

      R Offline
      R Offline
      rrrado
      wrote on last edited by
      #2

      Try to set "Want return" style in resource editor for edit control, i think NM_RETURN could start working then


      rrrado

      C 1 Reply Last reply
      0
      • C CodeBrain

        I have a single line edit control and a class derived from CEdit. Now I want that the CFormView class which contains the edit control gets informed if the user presses the Return key if the Edit control has the input focus. I've tried to react on the NM_RETURN notification in my form view but I don't get the event in the form view (must I use reflection in my edit class?). Then I tried to capture the VK_RETURN key in the OnChar method of my edit class, but the OnChar method seems not to get any VK_RETURN events. So what would be the best way to get my form view informed if the user presses the return key in my edit control? Thanks in advance.

        J Offline
        J Offline
        Johann Gerell
        wrote on last edited by
        #3

        Subclass the edit control like this:

        pOldEditWindowProc = (WNDPROC)::SetWindowLong(hWndEdit, GWL_WNDPROC, (LONG)EditWindowProc);
        .
        .
        .
        LRESULT CALLBACK EditWindowProc(HWND aHwnd, UINT aMsg, WPARAM aWp, LPARAM aLp)
        {
        if(Return was pressed) ::SendMessage(::GetParent(aHwnd), aMsg, aWp, aLp);

        return ::CallWindowProc(pOldEditWindowProc, aHwnd, aMsg, aWp, aLp);
        

        }

        -- Human beings, who are almost unique in having the ability
        to learn from the experience of others, are also remarkable
        for their apparent disinclination to do so. (Douglas Adams)

        1 Reply Last reply
        0
        • C CodeBrain

          I have a single line edit control and a class derived from CEdit. Now I want that the CFormView class which contains the edit control gets informed if the user presses the Return key if the Edit control has the input focus. I've tried to react on the NM_RETURN notification in my form view but I don't get the event in the form view (must I use reflection in my edit class?). Then I tried to capture the VK_RETURN key in the OnChar method of my edit class, but the OnChar method seems not to get any VK_RETURN events. So what would be the best way to get my form view informed if the user presses the return key in my edit control? Thanks in advance.

          R Offline
          R Offline
          Roger Allen
          wrote on last edited by
          #4

          The other way to handle this is in PreTranslateMessage() if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN && GetFocus() == GetDlgItem(ID_OF_CONTROL)) { // handle it here! } Roger Allen Sonork 100.10016 Were you different as a kid? Did you ever say "Ooohhh, shiny red" even once? - Paul Watson 11-February-2003

          C 1 Reply Last reply
          0
          • R rrrado

            Try to set "Want return" style in resource editor for edit control, i think NM_RETURN could start working then


            rrrado

            C Offline
            C Offline
            CodeBrain
            wrote on last edited by
            #5

            No luck, it seems not to work. I've already tried it and it makes no difference. The documentation also says that "Want return" only effects multiline edit controls.

            1 Reply Last reply
            0
            • R Roger Allen

              The other way to handle this is in PreTranslateMessage() if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN && GetFocus() == GetDlgItem(ID_OF_CONTROL)) { // handle it here! } Roger Allen Sonork 100.10016 Were you different as a kid? Did you ever say "Ooohhh, shiny red" even once? - Paul Watson 11-February-2003

              C Offline
              C Offline
              CodeBrain
              wrote on last edited by
              #6

              I have added some additional code, which seems to work now. But I still find it a bit ugly to perform the check in the application class. But that's ugly windows X| ;) Would be nice if someone could add feedback, if this code is correct or not, e.g. the CWinApp::PreTranslateMessage(pMsg); part. Thanks for your help! BOOL CMyApp::PreTranslateMessage(MSG* pMsg) { if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN)) { HWND hFocus = GetFocus(); CFrameWnd* pFrmWnd = dynamic_cast(GetMainWnd()); if (pFrmWnd == NULL) { ASSERT(FALSE); } CWnd* pTarget = pFrmWnd->GetActiveView()->GetDlgItem(IDC_EDIT_X); // We should test, if an active view exists, if we are not sure... HWND hTarget = NULL; if (pTarget != NULL) { hTarget = pTarget->GetSafeHwnd(); } if (hFocus == hTarget) { // Do what you want } return 1; } else { return CWinApp::PreTranslateMessage(pMsg); } }

              R 1 Reply Last reply
              0
              • C CodeBrain

                I have added some additional code, which seems to work now. But I still find it a bit ugly to perform the check in the application class. But that's ugly windows X| ;) Would be nice if someone could add feedback, if this code is correct or not, e.g. the CWinApp::PreTranslateMessage(pMsg); part. Thanks for your help! BOOL CMyApp::PreTranslateMessage(MSG* pMsg) { if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN)) { HWND hFocus = GetFocus(); CFrameWnd* pFrmWnd = dynamic_cast(GetMainWnd()); if (pFrmWnd == NULL) { ASSERT(FALSE); } CWnd* pTarget = pFrmWnd->GetActiveView()->GetDlgItem(IDC_EDIT_X); // We should test, if an active view exists, if we are not sure... HWND hTarget = NULL; if (pTarget != NULL) { hTarget = pTarget->GetSafeHwnd(); } if (hFocus == hTarget) { // Do what you want } return 1; } else { return CWinApp::PreTranslateMessage(pMsg); } }

                R Offline
                R Offline
                Ryan Binns
                wrote on last edited by
                #7

                I would have overridden PreTranslateMessage() in your edit control, and just sent a message to the parent:

                BOOL CMyEdit::PreTranslateMessage(MSG *pMsg)
                {
                if((pMsg->message == WM_KEYDOWN) && (pMsg->wParam == VK_RETURN))
                {
                CWnd *pParent = GetParent();
                if(pParent != NULL)
                pParent->SendMessage(WM_KEYDOWN, VK_RETURN, pMsg->lParam);
                }
                else
                return CEdit::PreTranslateMessage(pMsg);
                }

                And then just handle WM_KEYDOWN like normal in the parent and check for Enter being pressed. If you need to handle it in your main frame window, then use AfxGetMainWnd() instead of GetParent(). Ryan Being little and getting pushed around by big guys all my life I guess I compensate by pushing electrons and holes around. What a bully I am, but I do enjoy making subatomic particles hop at my bidding - Roger Wright (2nd April 2003, The Lounge)
                Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late - John Nichol "Point Of Impact"

                C 1 Reply Last reply
                0
                • R Ryan Binns

                  I would have overridden PreTranslateMessage() in your edit control, and just sent a message to the parent:

                  BOOL CMyEdit::PreTranslateMessage(MSG *pMsg)
                  {
                  if((pMsg->message == WM_KEYDOWN) && (pMsg->wParam == VK_RETURN))
                  {
                  CWnd *pParent = GetParent();
                  if(pParent != NULL)
                  pParent->SendMessage(WM_KEYDOWN, VK_RETURN, pMsg->lParam);
                  }
                  else
                  return CEdit::PreTranslateMessage(pMsg);
                  }

                  And then just handle WM_KEYDOWN like normal in the parent and check for Enter being pressed. If you need to handle it in your main frame window, then use AfxGetMainWnd() instead of GetParent(). Ryan Being little and getting pushed around by big guys all my life I guess I compensate by pushing electrons and holes around. What a bully I am, but I do enjoy making subatomic particles hop at my bidding - Roger Wright (2nd April 2003, The Lounge)
                  Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late - John Nichol "Point Of Impact"

                  C Offline
                  C Offline
                  CodeBrain
                  wrote on last edited by
                  #8

                  Thank you for the hint, I will try 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