Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 570
knaser Avatar asked Feb 12 '09 14:02

knaser


3 Answers

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

like image 113
SAMills Avatar answered Oct 03 '22 09:10

SAMills


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
}
like image 16
Aidan Ryan Avatar answered Oct 15 '22 09:10

Aidan Ryan


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;
}
like image 5
Mark Ingram Avatar answered Oct 15 '22 08:10

Mark Ingram