A problem when you press ESC in a CEdit ctrl
-
Everyone knows that when you press the 'Enter' key or 'ESC' key in a dialog application, even if in a CEdit ctrl that has focus, the dialog will exit. If you want to prevent this unwantable annoying default behavior, you should write some code. There is an atricle about how to catch those Enter keys in controls at CP. The author tell us to add a hanled to catch
WM_GETDLGCODE
message,if the function's return code isDLGC_WANTALLKEYS
, then our controls will know every message and we have the right to decide what to do to those 'Enter','ESC' keys. So I subclass a CEdit class called CMyEdit, and added two message handlers OnGetDlgCode and OnKeyDown. In CMyEdit's OnKeyDown function, I can catchVK_RETURN
andVK_ESCAPE
, and the dialog will not exit even if Enter or ESC is pressed in the edit ctrl. Hooray! ;P But!!! when I change this CMyEdit from singleline to multiline, everything changed! Now OnKeyDown function will not seeVK_ESCAPE
any more, butVK_RETURN
is still catched. And when the ESC is pressed in the ctrl, dialog exit. :mad:WhyDLGC_WANTALLKEYS
doesn't work now? After I googled for half a day. I found a solution--override CEdit's PreTranslateMessage and catch all key's there. I want to known whyDLGC_WANTALLKEYS
works well for single line CEdit but not for multiline? And which is the best way to prevent dialog from exiting when the ESC or Enter is pressed in a CEdit ctrl? Thank you all! :)A Chinese VC++ programmer
-
Everyone knows that when you press the 'Enter' key or 'ESC' key in a dialog application, even if in a CEdit ctrl that has focus, the dialog will exit. If you want to prevent this unwantable annoying default behavior, you should write some code. There is an atricle about how to catch those Enter keys in controls at CP. The author tell us to add a hanled to catch
WM_GETDLGCODE
message,if the function's return code isDLGC_WANTALLKEYS
, then our controls will know every message and we have the right to decide what to do to those 'Enter','ESC' keys. So I subclass a CEdit class called CMyEdit, and added two message handlers OnGetDlgCode and OnKeyDown. In CMyEdit's OnKeyDown function, I can catchVK_RETURN
andVK_ESCAPE
, and the dialog will not exit even if Enter or ESC is pressed in the edit ctrl. Hooray! ;P But!!! when I change this CMyEdit from singleline to multiline, everything changed! Now OnKeyDown function will not seeVK_ESCAPE
any more, butVK_RETURN
is still catched. And when the ESC is pressed in the ctrl, dialog exit. :mad:WhyDLGC_WANTALLKEYS
doesn't work now? After I googled for half a day. I found a solution--override CEdit's PreTranslateMessage and catch all key's there. I want to known whyDLGC_WANTALLKEYS
works well for single line CEdit but not for multiline? And which is the best way to prevent dialog from exiting when the ESC or Enter is pressed in a CEdit ctrl? Thank you all! :)A Chinese VC++ programmer
The simplest solution, working well, and you won't need to override anything of CEdit class:
void CTestDlg::OnBtnClickedCancel()
{ // [Cancel] key handler.
CWnd* pWnd = GetFocus();
if(pWnd == dynamic_cast<CWnd*>(&m_ctlEditMultiline)) {
// When the focus is in the multi-line edit control.
return; // Ignore.
}
OnCancel(); // Exit dialog box.
}Maxwell Chen
-
The simplest solution, working well, and you won't need to override anything of CEdit class:
void CTestDlg::OnBtnClickedCancel()
{ // [Cancel] key handler.
CWnd* pWnd = GetFocus();
if(pWnd == dynamic_cast<CWnd*>(&m_ctlEditMultiline)) {
// When the focus is in the multi-line edit control.
return; // Ignore.
}
OnCancel(); // Exit dialog box.
}Maxwell Chen
Yes, If you handle the ESC key in the upmost dialogbox, the there will be many if there. If the focus is in a edit box then blablala, and If the focus is in a listctrl then something else. This is why I appreciate handling the WM_GETDLGCODE message. Then each control has its own way to handle key message. I believe the code will be cleaner. :) But it doesn't works well for multiline CEdit ctrls. Any way, Thank you Maxwell Chen :)
A Chinese VC++ programmer
-
Yes, If you handle the ESC key in the upmost dialogbox, the there will be many if there. If the focus is in a edit box then blablala, and If the focus is in a listctrl then something else. This is why I appreciate handling the WM_GETDLGCODE message. Then each control has its own way to handle key message. I believe the code will be cleaner. :) But it doesn't works well for multiline CEdit ctrls. Any way, Thank you Maxwell Chen :)
A Chinese VC++ programmer
;P
Maxwell Chen