Solved

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

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

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