Loading and Activating an Accelerator Table

An application loads an accelerator-table resource by calling the LoadAccelerators function and specifying the instance handle to the application whose executable file contains the resource and the name or identifier of the resource. LoadAccelerators loads the specified accelerator table into memory and returns the handle to the accelerator table.

An application can load an accelerator-table resource at any time. Usually, a single-threaded application loads its accelerator table before entering its main message loop. An application that uses multiple threads typically loads the accelerator-table resource for a thread before entering the message loop for the thread. An application or thread might also use multiple accelerator tables, each associated with a particular application window. This type of application loads the accelerator table for the window each time the user activates the window.

Windows CE maintains accelerator tables for each application. An application can define any number of accelerator tables for use with its own windows. A unique 32-bit handle, HACCEL, identifies each table. However, only one accelerator table can be active at a time for a specified thread.

To activate an accelerator table, call the TranslateAccelerator function in the message loop associated with the thread message queue to process accelerator keystrokes for a specified thread. The handle of the accelerator table passed to the TranslateAccelerator function determines which accelerator table is active for a thread. This function also monitors keyboard input to the message queue, checking for key combinations that match an entry in the accelerator table. When TranslateAccelerator finds a match, it translates the keyboard input—that is, the WM_KEYUP and WM_KEYDOWN messages—into a WM_COMMAND or WM_SYSCOMMAND message. It then sends the message to the window procedure of the specified window. The WM_COMMAND message includes the identifier of the accelerator that caused TranslateAccelerator to generate the message. The window procedure examines the identifier to determine the source of the message, and then it processes the message accordingly.

The following code example shows how to call TranslateAccelerator from within a message loop.

while (GetMessage (&msg, NULL, 0, 0))
{
  if (!TranslateAccelerator (
                  g_hwndMain,   // Handle to the destination window.
                  hAccel,       // Handle to the accelerator table.
                  &msg))        // Address of the message data.
  {
    TranslateMessage (&msg);
    DispatchMessage (&msg);
  }
}

Note Unlike Windows-based desktop platforms, Windows CE does not maintain a system-wide accelerator table that applies to all applications.

To change the active accelerator table, pass a different accelerator-table handle to TranslateAccelerator.