Managing Per-Window Accelerator Tables

Last reviewed: November 2, 1995
Article ID: Q82171
The information in this article applies to:
  • Microsoft Windows Software Development Kit (SDK) versions 3.1
  • Microsoft Win32 Application Programming Interface (API) included with:

        - Microsoft Windows NT versions 3.5 and 3.51
        - Microsoft Windows 95 version 4.0
    

SUMMARY

In the Windows environment, an application can have several windows, each with its own accelerator table. This article describes a simple technique requiring very little code that an application can use to translate and dispatch accelerator key strokes to several windows. The technique employs two global variables, ghActiveWindow and ghActiveAccelTable, to track the currently active window and its accelerator table, respectively. These two variables, which are used in the TranslateAccelerator function in the application's main message loop, achieve the desired result.

MORE INFORMATION

The key to implementing this technique is to know which window is currently active and which accelerator table, if any, is associated with the active window. To track this information, process the WM_ACTIVATE message that Windows sends each time an application gains or loses activation. When a window loses activation, set the two global variables to NULL to indicate that the window and its accelerator table are no longer active. When a window that has an accelerator table gains activation, set the global variables appropriately to indicate that the accelerator table is active. The following code illustrates how to process the WM_ACTIVATE message:

case WM_ACTIVATE:

   if (wParam == 0)  // indicates loss of activation
   {
      ghActiveWindow = ghActiveAccelTable = NULL;
   }
   else              // indicates gain of activation
   {
      ghActiveWindow = <this window>;
      ghActiveAccelTable = <this window's accelerator table>;
   }
   break;

The application's main message loop resembles the following:

while (GetMessage(&msg,     // message structure
                  NULL,     // handle of window receiving the msg
                  NULL,     // lowest message to examine
                  NULL))    // highest message to examine
{
   if (!TranslateAccelerator(ghActiveWindow, // active window
                             ghActiveAccelTable, // active accelerator
                             &msg))
   {
      TranslateMessage(&msg); // Translates virtual key codes
      DispatchMessage(&msg);  // Dispatches message to
                              //   window procedure
   }
}

Under Windows version 3.1, the WM_ACTIVATE message with the wParam set to WA_INACTIVE indicates loss of activation.

Under Win32, the low-order word of wParam set to WA_INACTIVE indicates deactivation.


Additional reference words: 3.00 3.10 3.50 4.00 95
KBCategory: kbui
KBSubcategory: UsrMen


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: November 2, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.