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.
Printable View
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.
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);
}
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);
}
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 ?
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);
}