none
How to disable ESCAPE KEY of Keyboard in MFC applicatio?

    Question

  •  

    In my MFC dialog based application , when I execute program and press escape key of keyboard , my application terminates.

    I have deleted default OK and CANCEL buttons in my Form.

     

     

    Question 1)

    I don't want to terminate my program on pressing escape key. What should I do for this?

     

    Question 2)

    I have shown message box on close event of dialog box. Message box is shown when I click on cross which is on upper right side of dialog window. But it is not shown when my program is terminated by pressing escape  key of keyboard. This means that close event is not called in such case. So which event get called in this scenario, which terminates my program?

     

     

    Question 3)

      If it is not possible to disable escape key, then I want to handle this event. I want to save data before termination. So Where shoul I call my save function so that data will be saved even if I press escape key and terminate the program.


     

    Wednesday, October 24, 2007 1:00 PM

Answers

All replies

  • Simply handle OnCancel() inside your code that's all you have to do.

     

    Code Block

    void CTestAppaDlg::OnCancel()
    {
     // TODO: Add extra cleanup here
     
    // CDialog::OnCancel(); // Even if you just comment this line this will also work for you.
    }

     

     

    Thanx

    Wednesday, October 24, 2007 2:04 PM
  • But if you hit the close button of the dialog's title bar (X) the same message (IDCANCEL) gets sent. How can you differenciate between hitting the escape key and clicking on the afore-mentioned close button?

    Saturday, December 29, 2007 9:36 PM
  • To handle the clicking of the (X) button you can handle the WM_CLOSE message.

    I have a related but different problem however. I have overridden OnCancel to avoid ESCAPE killing my application, but I want to handle the key press in a child control. Other key presses get passed to the control but ESCAPE does not. How can I ensure that my control responds to ESCAPE being pressed?


    Thanks
    Thursday, November 26, 2009 12:16 PM
  • I have a related but different problem however. I have overridden OnCancel to avoid ESCAPE killing my application, but I want to handle the key press in a child control. Other key presses get passed to the control but ESCAPE does not. How can I ensure that my control responds to ESCAPE being pressed?
    BOOL CYourDlg::PreTranslateMessage(MSG* pMsg)
    {
    	switch ( pMsg->message )
    	{
    		case WM_KEYDOWN:
    		{
    			switch( pMsg->wParam )
    			{
    				case VK_ESCAPE:
    				case VK_CANCEL:
    					::DispatchMessage( pMsg );
    					return true;
    			}
    			break;
    		}
    	}
    	return CDialog::PreTranslateMessage(pMsg);
    }
    Thursday, November 26, 2009 12:43 PM
  • The codes from hgn will be the best I think, it is very useful. Thx.
    jack
    --------------------------------------------------------------
    E-XD++ Visualization Kit: http://www.ucancode.net


    jack
    Thursday, November 26, 2009 1:35 PM
  • Thank hgn. But I need to add my header file this;

    class CYourDlg : public CDialog
    {
    //...
    public:
    //...
    	afx_msg BOOL CYourDlg::PreTranslateMessage(MSG* pMsg);
    //...
    };

    Saturday, April 03, 2010 11:48 AM
  • Always try to avoid PreTranslateMessage() message in your code. THis is not a proper way to do the things.

     

    Thanks


    Rupesh Shukla
    Friday, April 16, 2010 6:53 PM