CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Guest

    capture ESC key in MFC

    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.


  2. #2
    Join Date
    Apr 1999
    Posts
    306

    Re: capture ESC key in MFC

    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);
    }


  3. #3
    Join Date
    May 1999
    Location
    Sydney, Australia
    Posts
    420

    Re: capture ESC key in MFC

    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);
    }











  4. #4
    Guest

    Re: capture ESC key in MFC

    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 ?


  5. #5
    Join Date
    May 1999
    Location
    Sydney, Australia
    Posts
    420

    Re: capture ESC key in MFC

    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);
    }





Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured