tags:

views:

846

answers:

3

How to handle return key (VK_RETURN) in a CEdit control? CEdit is lying in a CDialog.

A: 

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

SAMills
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.
knaser
Then you'll need to subclass the edit control and handle the WM_CHAR or KeyDown messages which are answered in other SO questions. OR within the dialog's MessageMap, handle Edit Change messages from the control (sent for every character).
SAMills
+2  A: 

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
    }
}
Aidan Ryan
A: 

Try overloading this method in your edit control class:

http://msdn.microsoft.com/en-us/library/3zzfkd75%28VS.80%29.aspx

anand.arumug