The Accelerator Table

Keyboard accelerator tables are defined in your .RC resource script. The general form is shown here:

MyAccelerators ACCELERATORS

{

[accelerator definitions]

}

This accelerator table name is MyAccelerators. The ACCELERATORS table does not include load and memory options. You can have multiple ACCELERATORS tables in your resource script.

Each keyboard accelerator you define requires a different line in the table. There are four types of accelerator definitions:

"char", wID [,NOINVERT] [,SHIFT] [,CONTROL]

"^char", wID [,NOINVERT] [,SHIFT] [,CONTROL]

nCode, wID, ASCII [,NOINVERT] [,SHIFT] [,CONTROL]

nCode, wID, VIRTKEY [,NOINVERT] [,SHIFT] [,CONTROL]

In these examples, "char" means a single character enclosed in double quotation marks, and "^char" is the character ^ and a single character in double quotation marks. The wID number performs a function similar to the menu ID in a menu definition. It is the value that Windows sends to your window procedure in the WM_COMMAND message to identify the accelerator. These are usually identifiers defined in a header file. When the keyboard accelerator duplicates a menu command, use the same ID for both the menu and the accelerator. When the keyboard accelerator does not duplicate a menu command, use a unique ID.

Keyboard accelerators almost always select options in popup menus. Windows automatically flashes a top-level menu item when you press an accelerator key that duplicates an option in a popup. (For example, the Edit text flashes if you press the Del key.) If you don't want the menu to flash, include the option NOINVERT.

In the first type of accelerator definition, the keyboard accelerator is a case-sensitive match of the character in double quotes:

"char", wID [,NOINVERT] [,SHIFT] [,CONTROL]

If you want to define a keyboard accelerator for that key in combination with the Shift or Ctrl key or both, simply add SHIFT or CONTROL or both.

In the second type of definition, the keyboard accelerator is the character in combination with the Ctrl key:

"^char", wID [,NOINVERT] [,SHIFT] [,CONTROL]

This type is the same as the first type when the CONTROL keyword is used with the character alone.

The third and fourth types use a number (nCode) rather than a character in quotes:

nCode, wID, ASCII [,NOINVERT] [,SHIFT] [,CONTROL]

nCode, wID, VIRTKEY [,NOINVERT] [,SHIFT] [,CONTROL]

This number is interpreted as either case-sensitive ASCII code or a virtual key code, depending on the ASCII or VIRTKEY keyword.

The most common keyboard accelerators are the second and fourth types. You use the second type for character keys in combination with Ctrl. For example, this defines an accelerator for Ctrl-A:

"^A", wID

Use the fourth type for virtual key codes such as function keys. This defines an accelerator for the Ctrl-F9 combination:

VK_F9, wID, VIRTKEY, CONTROL

The identifier VK_F9 is defined in WINDOWS.H as the virtual key code for the F9 key, so you have to include the statement:

#include <windows.h>

near the top of the resource script. The resource compiler defines an identifier named RC_INVOKED that causes much of WINDOWS.H to be ignored.

The first and third types of definition shown above are rarely used. If you want to use them, watch out for case-sensitivity. Windows does a case-sensitive match on the "char" or nCode based on the character you press. When you add the SHIFT keyword, Windows checks to see if the Shift key is depressed. This situation sometimes causes results you may not anticipate. For instance, if "char" is "A", the keyboard accelerator is invoked when you press the A key with the Shift key down or Caps Lock on, but not both. If you use "A" with SHIFT, the A key must be pressed with Shift down, but the accelerator can't be invoked at all when Caps Lock is on. Similarly, "a" by itself is a keyboard accelerator for the unshifted A key or for the A key with both Shift down and Caps Lock on. But "a" with SHIFT invokes the accelerator only when Shift is down and Caps Lock is on.

When you define keyboard accelerators for a menu item, you should include the key combination in the menu item text. The tab (\t) character separates the text from the accelerator so that the accelerators align in a second column. To notate accelerator keys in a menu, the CUA Advanced Interface Design Guide recommends the text Ctrl or Shift followed by a plus sign and the key—for instance:

F6

Shift+F6

Ctrl+F6