Click to See Complete Forum and Search --> : Trapping ESC in Dialog Boxes
triksterut
May 24th, 1999, 04:31 PM
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 jeremy@wasatchinc.com
Jeremy
Override OnCancel(), don't call CDialog::OnCancel() in that function.
yhkim
May 24th, 1999, 10:01 PM
CMyDialog::OnCancel()
{
return;
//CDialog::OnCancel();
}
if u don't want to close Dialog with Enter key, it is same.
CMyDialog::OnOK()
{
return;
//CDialog::OnOK();
}
Dan Haddix
May 25th, 1999, 12:12 AM
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.
Troy T
May 25th, 1999, 11:54 AM
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
Dan Haddix
May 26th, 1999, 12:33 AM
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.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.