954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Capture the Enter Key in an Edit Control

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.

complete
Junior Poster
153 posts since Dec 2005
Reputation Points: 17
Solved Threads: 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 , 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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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 );
}
vpsingh88
Newbie Poster
2 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You