Solved

How do I trap the ENTER key in a modal dialog?

Posted on 1998-05-25
5
402 Views
Last Modified: 2013-11-20
With all DOS based apps the user normally uses the ENTER key to move from field to field. I have a CEdit derived class I'm trying to get to work this way but The OnChar or OnKeyDown events don't process the ENTER key on a modal dialog. How do I do this in my CEdit control?
0
Comment
Question by:bsimser
  • 4
5 Comments
 
LVL 3

Accepted Solution

by:
tma050898 earned 20 total points
ID: 1314906
Override PreTranslateMessage in the dialog. Details coming...
0
 
LVL 3

Expert Comment

by:tma050898
ID: 1314907
bsimser,

Here is an example of how to override the PreTranslateMessage function to use the <Enter> key as a <Tab>. It evens makes the <Shift><Enter> key act as a <Shift><Tab> (moves to previous control in tab order).

BOOL CPreTransTestDlg::PreTranslateMessage(MSG* pMsg)
{  
 BOOL bHandledMsg = FALSE;

 switch (pMsg->message)
 {                      
  case WM_KEYDOWN:
  {
   // Get the CWnd* for the current child control
   CWnd* pwndCurrent = NULL;
   pwndCurrent = GetFocus();              

   // here you can compare pwndCurrent.GetSafeHwnd() to m_pEdit.GetSafeHwnd()
   // if you care that it was the m_pEdit control that had focus
   switch (pMsg->wParam)
   {
    case VK_RETURN:  
     if (::GetKeyState(VK_CONTROL) & 0x8000)
     {
      break;
     }

     // If valid...
     if (pwndCurrent)
     {            
      // If shift-enter or shift-tab move to the PREVIOUS control
      CWnd* pwndNew = NULL;
      if(::GetKeyState(VK_SHIFT) & 0x8000)
      {
       pwndNew = this->GetNextDlgTabItem(pwndCurrent,TRUE);
      }
      else // move to the NEXT control
      {                            
       pwndNew = this->GetNextDlgTabItem(pwndCurrent);
      }  

      if (pwndNew)
      {
       pwndNew->SetFocus();
      }
     }  

     // Inform Windows that we took care of it
     bHandledMsg = TRUE;
    break;

    default: break;
   } // switch (pMsg->wParam)
  } // WM_KEYDOWN
  break;

  default: break;                  
 } // switch (pMsg->message)                  

 // continue normal translation and dispatching            
 return (bHandledMsg ? TRUE : CDialog::PreTranslateMessage(pMsg));
}

Tom

0
 
LVL 3

Expert Comment

by:tma050898
ID: 1314908
BTW, I should probably mention that you will need to create the PreTranslateMessage override for your dialog via ClassWizard. Then, copy the code from my sample function above into your dialog's implementation of PreTranslateMessage.
0
 
LVL 1

Author Comment

by:bsimser
ID: 1314909
Thanks. I saw PreTranslateMessage but figured there might be a better way around it. In any case, I overrode the PreTranslateMessage of the CEdit control instead of the dialog so I don't have to code it for every dialog (there are several dozen screens).
0
 
LVL 3

Expert Comment

by:tma050898
ID: 1314910
You can do that, but now if you add other control types (e.g., combo boxes, list boxes, etc., they won't repond the same way. You might want to think about writing a base dialog class and putting the PreTranslageMessage in that class. Then, you would derived all of your dialog classes from that base dialog class.

Tom

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

840 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