Solved

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

Posted on 1998-05-25
5
397 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
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 …
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

762 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now