An accelerator table is an association from keyboard keys combinations with a command item. This can be a menu command item, but this is not necessarily the case. When the user presses the defined keyboard keys combination (Ctrl+Shift+N, for example), the window procedure associated with the accelerator table will receive a WM_COMMAND message, containing the numerical value that you associated with this keyboard combination.
For instance, when you want that Ctrl+Shift+N do something special in your application, you define an accelerator key like
1) In your header file:
#define MENU_STOPCALCULATING
3000
2) In your resource file:
"^N" MENU_STOPCALCULATING
SHIFT CONTROL
3) In your window procedure:
switch(message) {
...
case WM_COMMAND:
switch(LOWORD(wParam)) {
case MENU_STOPCALCULATING:
if (CalculationsStarted()) {
StopCals();
}
break;
}
}
You can have a
menu item somewhere, with an item 'Stop calculations" but this is not
mandatory. You receive from windows this message when you do not forget to add
the TranslateMessage step in that famous MainLoop of your window procedure.
You can edit this table graphically from Weditres. To call
the corresponding editor you should either use the menu item in the 'Objects'
bar, or use the directory tree. There, (if there is already a resource table
defined in the application), you will find your table. Just double click in its
name and the accelerator editor will be called automatically.
The editor for this resource type is very simple. It will
show you at startup a list of all defined items. You can perform the usual
actions (Add, Modify, and Delete), with three buttons at the lower right.
If there is no accelerator table defined, the editor will
automatically start the 'Add' action, supposing you are not very interested in
contemplating an empty list. The Add/Change dialog box edits a single line of
the accelerator table.
Instead of showing here yet another screen shot, I think it is better to spend more time explaining the concepts behind this.
Each entry in the table consists of:
· A numerical value. This is the result of pressing the combination of keys.
· An associated numerical identifier.
·
A set of flags that determine how the numerical value is
interpreted.
You can define either plain Ascii keys combinations, or you
can set the 'Virtual Key' flag, that indicates to Windows that the value is to
be interpreted as a value for a virtual key, i.e. a key like VK_F1, VK_BACK,
etc. All those values are defined in <windows.h>, and they give the
programmer a view of the keyboard that is independent from the type of keyboard
that is used.
It is important to be clear that the ASCII/VIRTUAL KEY
flags are alternatives (hence the radio buttons in the editor). You can only use
one of those.
You can add flags specifying:
CONTROL: That flag means that the CONTROL key has to be pressed for the keys combination to be valid.
SHIFT: Ditto but with the key Shift.
ALT: The same for the Alt key.
NOINVERT: This means that the menu item associated with the
same identifier should not be highlighted if it exists at all.
These flags are not alternatives, they can all be set for
the same key combination. They can be set/unset using the checkboxes in the
dialog box that edits each line of the table.
The accelerator resource is named following the same
conventions as the naming of all other resource types: either with a character
string or an identifier. You load the resource using the LoadAccelerators
window function that searches for the specified accelerator table.