Solved

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

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

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: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

751 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