Escape key

Posted on 1999-01-01
Last Modified: 2013-11-20
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?
Question by:abc021397
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

thresher_shark earned 100 total points
ID: 1327115
To solve this problem, you want to override the OnCancel member function.  Here is what you want to do:
1) Open up your dialog header file, named something like CMyDlg.h
2) Add:
   void OnCancel ();
   somewhere in the "public:" area.
3) Open up your dialog source code file, named something like CMyDlg.cpp
4) Add:
   void CMyDlg::OnCancel ()
   to the end of the file.
5) Recompile
6) Run and voila! No more escape key exiting.

If you have any further questions on how to implement this or are having any other difficulty, please feel free to ask.  Thanks!

Expert Comment

ID: 1327116
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?


Expert Comment

ID: 1327117
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 );

if ( nChar == VK_ESCAPE )



Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 1327118
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.

Expert Comment

ID: 1327119
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

Don't use OnKeyDown.  It sometimes for some odd reason doesn't capture keys.

Expert Comment

ID: 1327120
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.


Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Macro to save highlighted rows as a separate file 5 46
VBA "SendKeys" Syntax for Multiple Keystrokes 7 125
zeroMAx challenge 20 126
unix example issues 18 106
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question