Click to See Complete Forum and Search --> : capture ESC key in MFC


May 21st, 1999, 05:32 AM
I have to prevent the default ESC key action in MFC in a dialog box (do the same as the Cancel button): the Cancel Button must works normally, but if the user press the ESC key, nothing does happened.
How can I do that ?

Thanks in advance, Pascal.

ric
May 21st, 1999, 05:46 AM
Override your dialog PreTranslateMessage() function like this:

int CMyDialog::PreTranslateMessage(MSG* pMsg)
{
if(pMsg->message==WM_KEYDOWN && pMsg->wParam==27) return 0;
else return CDialog::PreTranslateMessage(pMsg);
}

sally
May 21st, 1999, 07:39 AM
With one small change ric's code becomes excellent:
I'd write it like this instead and it will be all honky-dory:


int CMyDialog::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE)
return 0;

return CDialog::PreTranslateMessage(pMsg);
}

Sally
May 21st, 1999, 07:39 AM
With one small change ric's code becomes excellent:
I'd write it like this instead and it will be all honky-dory:


int CMyDialog::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE)
return 0;

return CDialog::PreTranslateMessage(pMsg);
}

May 21st, 1999, 08:40 AM
Thanks to you, that's all good.
Just a little problem: when I do as you told me, the computer bips when I push on ESC. Why ? Is there a mean to avoid that ?

sally
May 22nd, 1999, 06:44 AM
Try this:

int CMyDialog::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE)
return 1; // nore the return value changed

return CDialog::PreTranslateMessage(pMsg);
}

Sally
May 22nd, 1999, 06:44 AM
Try this:

int CMyDialog::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE)
return 1; // nore the return value changed

return CDialog::PreTranslateMessage(pMsg);
}