Solved

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

Posted on 1998-03-19
5
244 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
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

708 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

13 Experts available now in Live!

Get 1:1 Help Now