Solved

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

Posted on 1998-05-25
5
405 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
[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
  • 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

Industry Leaders: 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

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

623 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