McNeel Wiki
Handling Enter and Escape from Modal Dialogs
edit · print · help · all topics
Main Pages

AccuRender

Bongo

Brazil r/s

Developer

Flamingo

Penguin

Rhino Blogs

Rhino

Search

Languages

Česky

Deutsch

English

Español

Français

Italiano

Polish

日本語

한국어

中文(繁體)

 
.
VersionRhino 4.0
SummaryPreventing modal dialogs from closing when the Enter or Escape key is pressed.

Question

How do I stop my MFC modal dialog box from closing when the user presses the Enter or Escape keys?

Answer

The first step is to, on your CDialog-drived class, override the CWnd::PreTranslateMessage virtual function. This function is used translate window messages before they are dispatched to the TranslateMessage and DispatchMessage Windows functions. Then, add the following block of code:

  BOOL CMyModalDialog::PreTranslateMessage( MSG* pMsg )
  {
    if( pMsg )
    {
      if( pMsg->message == WM_KEYDOWN )
      {
        if( pMsg->wParam == VK_RETURN || pMsg->wParam == VK_ESCAPE )
          pMsg->wParam = NULL;
      } 
    }
    // Call the base class PreTranslateMessage. In this example,
    // CRhinoDialog is the base class to CMyModalDialog.
    return CRhinoDialog::PreTranslateMessage( pMsg );
  }
rename · changes · history · subscriptions · lost and found · references · file upload