Select the product you need help with
- Internet Explorer
- Windows Phone
- More products
How to use accelerator keys within a modal dialog box in Visual C++
Article ID: 222829 - View products that this article applies to.
This article was previously published under Q222829
Note Microsoft Visual C++ .NET (2002) supports both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model. The information in this article applies only to unmanaged Visual C++ code.
Accelerator keys are a common User Interface feature of Windows applications; therefore, why limit them to just the application? This article shows how to add accelerator key functionality to any modal dialog box.
Keyboard accelerators are processed by calling the TranslateAccelerator() function in the application's main message loop. However, for a modal dialog box, the dialog box manager message loop (built into Windows) is used to translate and dispatch messages. Of course, because this message loop is not designed to process accelerators, it does not call the TranslateAccelerator() function.
To process accelerator keys in a modal dialog box, you must override the dialog box's PreTranslateMessage() function and try to process the message as an accelerator by calling ::TranslateAccelerator(). If this method fails, then processing continues by calling the base class PreTranslateMessage().
For the purposes of this article, we add accelerator key functionality to the AboutBox dialog box of an MFC MDI application:
Results Accelerator key F5 now triggers the button1 handler when the CAboutBox is active.
For additional information about accelerator key processing within modal dialog boxes, please see the following article in the Microsoft Knowledge Base:
(http://support.microsoft.com/kb/100770/EN-US/ )INFO: Using Accelerator Keys with Modal Dialog Box Main Window
Article ID: 222829 - Last Review: November 21, 2006 - Revision: 4.1