Solved

Activating the OK and CANCEL buttons on a dialog box using the F1 and F12 keys

Posted on 1998-03-19
5
249 Views
Last Modified: 2010-04-10
I need to be able to let the user hit the F1 key to activate the CANCEL button and the F12 key to activate the OK button.

I tried setting up an accelerator table to do this, but I only seem to get the accelerator table to work with my MAIN dialog window,  

This is a POP-UP.

Any ideas on how to do this??
0
Comment
Question by:azar
[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
5 Comments
 
LVL 4

Expert Comment

by:tflai
ID: 1183644
Add your handler for PreTranslateMessage and catch the key pressed there:
----------
BOOL CMyDlg::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_KEYDOWN)
{
switch(pMsg->wParam)
{
case VK_F1:
CDialog::OnCancel();  
return TRUE;
case VK_F12:
CDialog::OnOK();
return TRUE;
}
}
return CDialog::PreTranslateMessage(pMsg);
}
0
 

Author Comment

by:azar
ID: 1183645
That is how I originally tried it and nothing happens - e.g. the function key is never seen by the code.

I tried BOTH that apporach and another - see the code below (I commented out the first try).

NEITHER work for the pop-up windows - only for the main dialog box.

Any idea why?


/////////////////////////////////////////////////////////////////////////////
// NewPatient messCatheterLength handlers

/*
 * Accelerator table data structure. This has been set up arbitrarily
 * and may be altered depending on the desired functionality.
 */
#define TABLELENGTH 2

typedef struct {
    BYTE cChar;      
    BYTE cVirtKey;
    WORD wID;
} Accelerator;


/*
 * Array of accelerator keys. For demonstration purposes, these values
 * are predefined. An application could read in the
 * user's keystrokes and assign these values at run time or from an
 * INI file.
 */

#define VKCTRL 5
#define VKSHFT 6
#define IDD_UNDEFINED  999


Accelerator NPAccTable[TABLELENGTH] =   {
                {NULL, VK_F12, IDOK},                          //F12 will activate the Exit Button
                {NULL, VK_F1, IDCANCEL},                    //F1 will activate the Cancel Button
                                      };

/////////////////////////////////////////////////////////////////////////////
// dpp message handlers

BOOL NewPatient::ActivateMenuItem(HWND hWnd,
                      WORD wID)
{
      SendMessage(WM_COMMAND, wID, 0L);
      return TRUE;  //key combination found and processed
}

UINT NewPatient::OnGetDlgCode()
{  
      return CDialog::OnGetDlgCode()|DLGC_WANTMESSAGE;
//      return CDialog::OnGetDlgCode()|DLGC_WANTALLKEYS;
}  



BOOL NewPatient::PreTranslateMessage(MSG* pMsg)
{
   if (pMsg->message == WM_KEYDOWN)
         {
         switch(pMsg->wParam)
               {
               case VK_F1:
                     CDialog::OnCancel();  
                     return TRUE;
               case VK_F12:
                     CDialog::OnOK();
                     return TRUE;
               }
      }
   return CDialog::PreTranslateMessage(pMsg);
}                                            
                                 

           
BOOL NewPatient::CheckSpecialKeys(MSG* lpMsg)
{
      int ii = 0;
      ii++;
      if (lpMsg->message == WM_KEYDOWN)  
      {  
            ii++;

        // Since the user has hit a key, see if it is in our table of special ones
               for (int i = 0; i < TABLELENGTH; i++)  //check table for a match
            {
          // See if the key the user hit is in the table of special ones
              if ((lpMsg->wParam == NPAccTable[i].cVirtKey) && (NPAccTable[i].wID != IDD_UNDEFINED))  // key is in the table and is defined
          {
                int ii = 4;
               switch (NPAccTable[i].cVirtKey)
            {
                  case VK_F1:  //is F1 down?
                         // Cancel any changes made
                         ActivateMenuItem(this->m_hWnd, NPAccTable[i].wID);
                         OnCancel();
                              return TRUE;
                  break;

                  case VK_F12:  //is F10 down?
                              // Return Happy  
                              CDialog::OnOK();
                              return TRUE;
                     break;
            } //end switch
              }
            } //end for loop
      }
         return FALSE;    //key combination not found in accelerator table
}

//   BOOL NewPatient::PreTranslateMessage( MSG* pMsg )
//{                  
//      int ii = 0;
//      ii++;
//      if (CheckSpecialKeys(pMsg)) /* application defined */
//          return TRUE;            
//    else
//         return CDialog::PreTranslateMessage( pMsg );
//}

0
 
LVL 4

Accepted Solution

by:
tflai earned 50 total points
ID: 1183646
A popup modal dialog box handles its own message and doesn't pass back up to the parent.  I think that's your problem.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1183647
You can subclass the dialog.
See http://www.frogpond.org/~markg

0
 

Expert Comment

by:alan93
ID: 3646061
This worked for me.  Allows your Dialog to read the accelerator table.

BOOL CYourDlg::PreTranslateMessage(MSG* pMsg)
{

            CYourApp *pDoc = (CYourApp*)AfxGetApp();
if (!(pDoc->ghAccelTable && ::TranslateAccelerator(m_hWnd,pDoc->ghAccelTable,pMsg)))
 {
            return CDialog::PreTranslateMessage(pMsg);
      
 }
else

      return TRUE;
      return CDialog::PreTranslateMessage(pMsg);
}
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

705 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