How do I capture the Enter Key event in an edit control?

I have so far inherited my own class from CEdit and I am capturing the input keys but it the method used to do this does not receive the event when the enter or return key is hit.

The first step in solving this is to configure the dialog box so that when the user hits the enter key, the dialog box does not close. I have done this already: http://msdn.microsoft.com/msdnmag/issues/0700/c/default.aspx

But how do I do the next step and capture that enter key when the user is done entering data in an edit control.

Recommended Answers

All 2 Replies

The way I did it was subclass CEdit then enable the OnPreTranslate() function. In that function you can check if the key is the <Enter>, and if it is just return (I think TRUE) so that it gets ignored. I don't have a compiler with MFC to verify, but should be close.

Found this...

Use PreTranslateMessage to trap the WM_KEYDOWN/VK_RETURN and test if it is
for the edit control in question.

BOOL CTestDlg::PreTranslateMessage( MSG* pMsg )
{
// TODO: Add your specialized code here and/or call the base class
if ( pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN )
{
int idCtrl= this->GetFocus()->GetDlgCtrlID();
  if ( idCtrl == IDC_EDIT1 )
  {
    MessageBox( _T("Got it!") );
  }
}
return CDialog::PreTranslateMessage( pMsg );
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.