How can I handle the Return key in a CEdit control?

18,476

Solution 1

You could also filter for the key in your dialog's PreTranslateMessage. If you get WM_KEYDOWN for VK_RETURN, call GetFocus. If focus is on your edit control, call your handling for return pressed in the edit control.

Note the order of clauses in the if relies on short-circuiting to be efficient.

BOOL CMyDialog::PreTranslateMessage(MSG* pMsg)
{
    if (pMsg->message == WM_KEYDOWN &&
        pMsg->wParam == VK_RETURN &&
        GetFocus() == m_EditControl)
    {
        // handle return pressed in edit control
        return TRUE; // this doesn't need processing anymore
    }
    return FALSE; // all other cases still need default processing
}

Solution 2

The correct answer is to handle the WM_GETDLGCODE / OnGetDlgCode message. In there you can specify that you want all keys to be handled by your class.

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

Solution 3

Make certain the Edit Control style ES_WANTRETURN is set in the dialog resource for the control

Solution 4

By default, the Return key closes an MFC dialog. This is, because the Return key causes the CDialog's OnOK() function to be called. You can override that function in order to intercept the Return key. I got the basic idea from this article (see Method 3 at the end).

First, make sure that you have added a member for the edit control to your dialog using the Class Wizard, for example:

CEdit m_editFind;

Next, you can add the following function prototype to the header file of your dialog:

protected:
    virtual void OnOK();

Then you can add the following implementation to the cpp file of your dialog:

void CMyDialog::OnOK()
{
    if(GetFocus() == &m_editFind)
    {
        // TODO: Add your handling of the Return key here.
        TRACE0("Return key in edit control pressed\n");

        // Call `return` to leave the dialog open.
        return;
    }

    // Default behavior: Close the dialog.
    CDialog::OnOK();
}

Please note: If you have an OK button in your dialog which has the ID IDOK, then it will also call OnOK(). If this causes any problems for you, then you have to redirect the button to another handler function. How to do this is also described in Method 3 of the article that I have mentioned above.

Solution 5

I encounter this problem myself. After a little experiment, a simple way existed if you just want to get the do something (after some editing etc) on the return (not specific for which editor you have focus) - I would just create a invisible default button, and let that button handle the 'return' key instead of default Ok button (of course, Ok button should be set default key to false)

Share:
18,476
knaser
Author by

knaser

Updated on June 14, 2022

Comments

  • knaser
    knaser almost 2 years

    How can I handle the Return key (VK_RETURN) in a CEdit control? The CEdit control is parented to a CDialog.

  • knaser
    knaser over 15 years
    This does not work. This is not what I want. I want to get RETURN key AND do something. Not just allow entering RETURN key.
  • Adam McKee
    Adam McKee about 13 years
    ES_WANTRETURN is only applicable for multi-line text boxes.
  • aquirdturtle
    aquirdturtle almost 8 years
    While not what the op was looking for (perhaps edit answer to reflect that), this was exactly what I was looking for. Thanks.