How can I handle the Return key (VK_RETURN
) in a CEdit
control? The CEdit
control is parented to a CDialog
.
Make certain the Edit Control style ES_WANTRETURN is set in the dialog resource for the control
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
}
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With