How to trap the ESCAPE key

Posted on 1998-12-11
Last Modified: 2013-11-20

I'm trying to trap the escape key but unsuccessfuly.
This is what I've tried:

CMyView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlag)
   if (nChar==VK_ESCAPE)
        //Some code...


However, when the escape key is pressed OnKeyDown message
is not called.

Any idea?
Question by:qocarlos

Expert Comment

ID: 1326063
I don't know for sure, but is it possible that the OnKeyDown member function is not called in the view class?  Have you tried the same code in the app class (is that the term?)?

Accepted Solution

pagladasu earned 50 total points
ID: 1326064
You should trap the PreTranslateMessage of your view class. Here is a sample code. It works for CView as well as CFormView derived classes:
BOOL CMyView::PreTranslateMessage(MSG* pMsg)
      // TODO: Add your specialized code here and/or call the base class
      return CFormView::PreTranslateMessage(pMsg);

I hope this will help. Thanks

Author Comment

ID: 1326065
THANKS, it works great!
What I would like to know is why I don't get the OnKeyDown function when pressing the escape key.
Anyway, thanks again for your help.

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error on moodle after upgrade 3 131
Separate into files by filename 12 83
unable to delete all specified values regedit 38 424
Problem to App 4 83
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: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

821 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