[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
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
?
252 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 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

834 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