Override Enter/Esc keys in the CDialog

Posted on 2003-02-21
Medium Priority
Last Modified: 2013-11-20
I need to override the behaviour of my CDialog-derived class when Enter or Esc key is pressed, but this event is not caught in CDialog::OnKeyDown(), and, although these keys generally hide the dialog, CWnd::OnWindowShow() is never called either. What message handler should I look for?
Question by:milchenko
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

Expert Comment

ID: 7995985
You have to override OnOK and OnCancel

Expert Comment

ID: 7996450

Expert Comment

ID: 7997660
u can override OnOk() for Enter & OnCancel()for Esc.This look like
   void CMyDialog::OnOK()
   void CMyDialog::OnCancel()
this can be done by simply add a two methods in class wizard as in the above
LVL 30

Accepted Solution

SteveGTR earned 400 total points
ID: 7997896
Or you can look at PreTranslateMessage():

BOOL CYourDlg::PreTranslateMessage(MSG* pMsg)
  if (pMsg->message == WM_KEYDOWN && 
      (pMsg->wParam == VK_ESC || pMsg->Param == VK_ENTER)
    return TRUE;

  return CDialog::PreTranslateMessage(pMsg); // Or parent class if not CDialog

Good Luck,

Author Comment

ID: 8001644
Thanks, this looks like the best code that would work for all cases.

Featured Post

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!

Question has a verified solution.

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

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

770 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