Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to prevent an MFC dialog from handling the enter and escape keys and not passing it on.when we enter in dialog ,apllication has closed.
Posted

You may override PreTranslateMessage():
C++
BOOL CMyDialog::PreTranslateMessage(MSG* pMsg)
{
    BOOL bRet = CDialog::PreTranslateMessage(pMsg);
    if (pMsg->message == WM_KEYDOWN &&
        (pMsg->wParam == VC_RETURN || pMsg->wParam == VK_ESCAPE))
        bRet = TRUE; // do not dispatch message
    return bRet;
}

But you should think about it. When using this, the user can not close the dialog using the keyboard.
 
Share this answer
 
Yeah, I hate it when somebody's typing in an edit box and naturally hits the "return" key and closes the dialog.

"Return" comes through to your dialog as the "OnOK" callback. If you're not handling the "OnOK" in your code (like you don't have an "OK" button displayed), you can simply provide an OnOK() handler that does nothing (do not call CDialog::OnOK())

void MyDialog::OnOK()
{
// I have no OK button so don't let default <cr> close the app
}</cr>
 
Share this answer
 
Escape gets mapped to the IDCANCEL button. So give that button a different ID (if you have a cancel button). Then in the IDCANCEL handler, just return, rather than call CDialog::OnCancel(). Map you renamed Cancel button to a handler that calls the parent dialog class OnCancel() member.

You'll see that the IDOK button is the default button in properties. Turn that off, and the 'Enter' key won't activate that button unless it has focus.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900