The article also discusses how to change the behaviour of <Enter> and other keys, providing a sample application with source code. Please refer to the article for details.
Another workaround can be achieved by overriding 'OnOK()':
Code:
void CYourDialog::OnOK(void)
{
CWnd* pWnd = GetFocus();
if(GetDlgItem(IDOK) == pWnd)
{
CDialog::OnOK();
return;
}
// Enter key was hit -> do whatever you want
}
This solution is a quick-fix, because it lacks on generality.
Another 'hack' that seems to be very popular (but not recommendable) is overriding 'PreTranslateMessage()':
Code:
BOOL CYourDialog::PreTranslateMessage(MSG* pMsg)
{
// ENTER key
if((pMsg->message == WM_KEYDOWN) &&
(pMsg->wParam == VK_RETURN))
{
// Enter key was hit -> do whatever you want
return TRUE;
}
return CDialog::PreTranslateMessage(pMsg);
}
This is a 'hack' because it breaks the modularity of MFC. If you want to change the behaviour of more keys but just <Enter> you will end up with a labyrinth of 'if' statements in the overriden 'PreTranslateMessage()' function. This is ugly, hard to read, hard to maintain and error prone. The MFC way of doing it is presented in detail in DiLascias' Article.
For further information on the whole topic, take also a look at the article 'Processing Keyboard Messages ' written by Sam Hobbs.
Last edited by Andreas Masur; July 24th, 2005 at 04:31 PM.
Bookmarks