Link to home
Start Free TrialLog in
Avatar of abc021397
abc021397

asked on

Escape key

Also, in my program, the {Esc} key will exit it.  I do not want this to happen.  I want the escape key to do nothing.  What can I do to disable this stupid escape key?
ASKER CERTIFIED SOLUTION
Avatar of thresher_shark
thresher_shark

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pagladasu
pagladasu

thresh,
Now suppose there is the Cancel command button with the ID value of IDCANCEL. OnCancel will be mapped to the clicked event of this button. Now overriding the OnCancel will not only disable the Escape key but will also prevent the user from exiting if the Cancel button is clicked. How about only disabling the escape key?

overridding OnCancel may create a problem while exiting on Cancel Button.

The Best way is in OnKeyDown

afx_msg void OnKeyDown( UINT nChar, UINT nRepCnt, UINT nFlags );
{
//compare

if ( nChar == VK_ESCAPE )
   return;
else

BaseClass::OnKeyDown

}

Thank you dd_b for providing the code.  Here it was night time which is why I did not respond sooner.

abc - If you can use either method to cancel the esc key's effect.  As has been shown, the second method is better.
NOO not OnKeyDown

PreTranslateMessage(MSG pMsg)
{
   if (GetAsyncKeyState(VK_ESCAPE))
     {
        do what you wan't to do to kill the button;
        I would probably say
        VK_ESCAPE=0;
     }
     
}

Don't use OnKeyDown.  It sometimes for some odd reason doesn't capture keys.
The_Brain, you're right. There are times when OnKeyDown will not work. I think PreTranslateMessage will work.
However as suggested by you VK_ESCAPE=0 will not work. VK_ESCAPE is already a predefined constant. You cannot change its value.