To handle a click on a menu item,
add an event listener for the
select
event to the
NativeMenuItem object:
var menuCommandX = new NativeMenuItem("Command X");
menuCommand.addEventListener(air.Event.SELECT, doCommandX)
Because
select
events bubble up to the containing
menus, you can also listen for select events on a parent menu. When
listening at the menu level, you can use the event object
target
property
to determine which menu command was selected. The following example
traces the label of the selected command:
var colorMenuItem = new air.NativeMenuItem("Choose a color");
var colorMenu = new air.NativeMenu();
colorMenuItem.submenu = colorMenu;
var red = new air.NativeMenuItem("Red");
var green = new air.NativeMenuItem("Green");
var blue = new air.NativeMenuItem("Blue");
colorMenu.addItem(red);
colorMenu.addItem(green);
colorMenu.addItem(blue);
if(air.NativeApplication.supportsMenu){
air.NativeApplication.nativeApplication.menu.addItem(colorMenuItem);
air.NativeApplication.nativeApplication.menu.addEventListener(air.Event.SELECT,
colorChoice);
} else if (air.NativeWindow.supportsMenu){
var windowMenu = new air.NativeMenu();
window.nativeWindow.menu = windowMenu;
windowMenu.addItem(colorMenuItem);
windowMenu.addEventListener(air.Event.SELECT, colorChoice);
}
function colorChoice(event) {
var menuItem = event.target;
air.trace(menuItem.label + " has been selected");
}
If you are using the ContextMenuItem class, you can listen for
either the
select
event or the
menuItemSelect
event.
The
menuItemSelect
event gives you additional information
about the object owning the context menu, but does not bubble up
to the containing menus.