Solved

Escape key

Posted on 1999-01-01
6
705 Views
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?
0
Comment
Question by:abc021397
[X]
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
6 Comments
 
LVL 6

Accepted Solution

by:
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!
0
 
LVL 4

Expert Comment

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

0
 
LVL 1

Expert Comment

by:dd_b
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 );
{
//compare

if ( nChar == VK_ESCAPE )
   return;
else

BaseClass::OnKeyDown

}

0
Technology Partners: 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!

 
LVL 6

Expert Comment

by:thresher_shark
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.
0
 
LVL 1

Expert Comment

by:The_Brain
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
        VK_ESCAPE=0;
     }
     
}

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

Expert Comment

by:pagladasu
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.


0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

688 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