0

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.

3
Contributors
2
Replies
3
Views
10 Years
Discussion Span
Last Post by vpsingh88
0

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.

1

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 );
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.