CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 1999
    Posts
    2

    Trapping ESC in Dialog Boxes

    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


  2. #2
    Guest

    Re: Trapping ESC in Dialog Boxes

    Override OnCancel(), don't call CDialog::OnCancel() in that function.


  3. #3
    Join Date
    May 1999
    Posts
    3

    Re: Trapping ESC in Dialog Boxes

    CMyDialog::OnCancel()
    {
    return;
    //CDialog::OnCancel();
    }

    if u don't want to close Dialog with Enter key, it is same.
    CMyDialog::OnOK()
    {
    return;
    //CDialog::OnOK();
    }



  4. #4
    Join Date
    May 1999
    Posts
    82

    Re: Trapping ESC in Dialog Boxes

    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.


  5. #5
    Join Date
    May 1999
    Location
    WA
    Posts
    65

    Re: Trapping ESC in Dialog Boxes

    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

  6. #6
    Join Date
    May 1999
    Posts
    82

    Re: Trapping ESC in Dialog Boxes

    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.


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