?
Solved

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

Posted on 1998-03-19
5
Medium Priority
?
250 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 100 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.
Suggested Courses

752 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