You can specify a key equivalent (sometimes called an accelerator)
for a window or application menu command. When the key or key combination
is pressed the NativeMenuItem dispatches a
select
event
and any
onSelect
event handler specified in the
data source is called. The behavior is the same as though the user had
selected the menu item.
For complete details about menu keyboard equivalents, see
Key equivalents for native menu commands (AIR)
.
Using the MenuBuilder framework, you can specify a keyboard equivalent
for a menu item in its corresponding node in the data source. If
the data source has a
keyEquivalent
field, the
MenuBuilder framework uses that value as the key equivalent character.
You can also specify modifier keys that are part of the key equivalent
combination. To add a modifier, specify
true
for
the
altKey
,
ctrlKey
,
cmdKey
,
or
shiftKey
field. The specified key or keys become
part of the key equivalent combination. By default the Control key
is specified for Windows and the Command key is specified for Mac
OS X. To override this default behavior, include a
defaultKeyEquivalentModifiers
field
set to
false
.
The following example shows the data structure for an XML-based
menu data source that includes keyboard equivalents, in a file named
“keyEquivalentMenu.xml”:
<?xml version="1.0" encoding="utf-8" ?>
<root>
<menuitem label="File">
<menuitem label="New" keyEquivalent="n"/>
<menuitem label="Open" keyEquivalent="o"/>
<menuitem label="Save" keyEquivalent="s"/>
<menuitem label="Save As..." keyEquivalent="s" shiftKey="true"/>
<menuitem label="Close" keyEquivalent="w"/>
</menuitem>
<menuitem label="Edit">
<menuitem label="Cut" keyEquivalent="x"/>
<menuitem label="Copy" keyEquivalent="c"/>
<menuitem label="Paste" keyEquivalent="v"/>
</menuitem>
</root>
The following example application loads the menu structure from
“keyEquivalentMenu.xml” and uses it as the structure for the window
or application menu for the application:
<html>
<head>
<title>XML-based menu with key equivalents example</title>
<script type="text/javascript" src="AIRAliases.js"></script>
<script type="text/javascript" src="AIRMenuBuilder.js"></script>
</head>
<body>
<script type="text/javascript">
// Create a NativeMenu from "keyEquivalentMenu.xml" and set it
// as the application/window menu
var keyEquivMenu = air.ui.Menu.createFromXML("keyEquivalentMenu.xml");
air.ui.Menu.setAsMenu(keyEquivMenu);
</script>
</body>
</html>