|
|
< Day Day Up > |
|
2.4 AcceleratorsA menu accelerator, also called a keyboard shortcut, is an application-defined key sequence that invokes a menu item. It gives the same result as though the user had selected the item with the mouse. Accelerators are always associated with menu items. In the same manner as items in the menu bar, accelerators are global to the window. When the user types an accelerator key, regardless of the focus control, a menu item is invoked. When an accelerator is triggered, the corresponding keystroke is consumed.[17]
2.4.1 Specifying an AcceleratorAccelerators are represented in SWT using an integer encoding. The encoding consists of zero or more modifiers and a single character or key code. An accelerator that contains only modifiers is invalid and cannot trigger a menu item. Table 2.12 shows some sample accelerators.
Accelerators are assigned to menu items using the method MenuItem.setAccelerator(). The Tool Bars and Menus chapter contains a complete description of the MenuItem class.
The following code fragment creates an accelerator for the primary modifier SWT.MOD1 and the 'A' character and associates it with a menu item.
item.setText("Select &All\tCtrl+A");
item.setAccelerator(SWT.MOD1 + 'A');
item.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
System.out.println("The item was selected.");
}
});
A perceptive reader should immediately notice a problem. The menu item text contains the string "Ctrl+A" but the accelerator is SWT.MOD1 + 'A'. Does this imply that this code fails on the Macintosh where the primary modifier is not the <Ctrl> key? To answer this question, you need to understand the difference between the accelerator and the accelerator text. 2.4.2 Specifying the Accelerator TextTo allow complete control over the label of a menu item, the accelerator text is specified independently of the accelerator.[18] An embedded \t character indicates that the following string is the accelerator text. On most platforms, the accelerator text is right-aligned within the menu.
When both an accelerator and accelerator text are provided, depending on the platform, SWT may override the accelerator text with more appropriate accelerator text. For example, on the Macintosh, the accelerator text "Ctrl+A" is ignored when the accelerator SWT.MOD1 + 'A' is specified. The correct accelerator text, a sequence of Macintosh specific glyphs, appears in the menu.
|
|
|
< Day Day Up > |
|