AIR FlexNativeMenu with platform independent keyboard shortcuts

I have build a little AIR app last weekend to try out a few things with the native menu feature of AIR. I recommend reading Developing Adobe AIR Applications with Adobe Flex 3 which is available in from the Adobe AIR Resources. The part about the FlexNativeMenu control starts at page 46 (53 in PDF).

In my AIR app I wanted to have platform specific keyboard shortcuts meaning CTRL+Key on Windows and CMD(Apple)+Key. Checking the livedocs I found FlexNativeMenu->keyEquivalentModifiersFunction which seems to handle “platform independence”.

I found the part “Using multi-platform key equivalent menu commands” in the PDF on page 57 (64 in PDF). The example works great! You have to define the isWin and isMac variables and set the keyEquivalentModifiers function in the FlexNativeMenu tag. Doing this you have now platform specific or independent (depends on your point of view) implementation of your keyboard shortcuts.

For my example I wanted to disable some menu items depending on the state of my application. When a file is for instance not saved yet or not modified you can only use “Save As …” and not “Save” so how can this be handled?

Listen the menuShow event from the FlexNativeMenu and do something like this in the event handler:

// FlexNativeMenu "menuShow" event handler
private function onShow(e:Event):void
{
// ModelLocator could be a Cairngorm ModelLocator
var document:MyDocument = ModelLocator.getInstance().document;
// myMenuData is the defined XML dataProvider for our FlexNativeMenu
myMenuData.menuitem.(@label == "File").menuitem.(@label == "Save").@enabled =  document.filename != null && document.changed;
}

With this simple E4X statement the right node is selected and the enabled attribute is set according to our application state/status.

2 comments to AIR FlexNativeMenu with platform independent keyboard shortcuts