Hello all,
Does anyone know how to trap the ESC key in a dialog box so that it does not close the dialog?
I would appreciate any leads at all! :) Please email [email protected]
Jeremy
Printable View
Hello all,
Does anyone know how to trap the ESC key in a dialog box so that it does not close the dialog?
I would appreciate any leads at all! :) Please email [email protected]
Jeremy
Override OnCancel(), don't call CDialog::OnCancel() in that function.
CMyDialog::OnCancel()
{
return;
//CDialog::OnCancel();
}
if u don't want to close Dialog with Enter key, it is same.
CMyDialog::OnOK()
{
return;
//CDialog::OnOK();
}
Actually doing this will prevent the Cancel button from working at all!!! What you need to do is to overload PreTranslateMessage and impliment it like this...
BOOL CYourDlg::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_KEYDOWN)
if (pMsg->wParam == VK_ESCAPE)
return TRUE;
return CDialog::PreTranslateMessage(pMsg);
}
This will give you the desired effect without completely disabling the cancel button.
Why not write it like the following for enhanced efficiency and better readability?
BOOL CYourDlg::PreTranslateMessage(MSG* pMsg)
{
if(pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE)
return TRUE;
return CDialog::PreTranslateMessage(pMsg);
}
- Troy
How does that make it more readable??? I think it is more confusing to have multiple && statments in an if statement. Not oly that but my method allows you to check for other keys by simply adding another if statment. For example
BOOL CYourDlg::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_KEYDOWN){
if (pMsg->wParam == VK_ESCAPE)
return TRUE;
if (pMsg->wParam == VK_RETURN)
// Do something for the enter key
}
return CDialog::PreTranslateMessage(pMsg);
}
would also trap the enter key and allow you to process them seperatly.