Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 1998-05-25
5
Medium Priority
?
407 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 80 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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 brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

722 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