Blograby

Setting Back, Menu, and Search Buttons

The system uses the buttons at the base of your device for navigation and search functions. Except for the home button, you can override their default behavior to use them in your AIR application.

Note that AIR currently cannot communicate with the native user interface and does not have access to the Options menu triggered by pressing the Menu key. The Options menu can hold up to six items; if additional items are stored, it displays a More menu item which reveals an expanded menu. If you want to create a native-like look and feel, you can design a similar-looking menu in your application.

Register for a keyboardEvent.KEY_DOWN event and track which soft key was pressed by identifying its keyCode. Call event.preventDefault() to catch the event before it triggers the default navigation and replace it with your desired behavior:

import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKey);
function onKey(event:KeyboardEvent):void {
switch (event.keyCode) {
case Keyboard.BACK:
event.preventDefault();
trace(“go back within the AIR application”);
break;
case Keyboard.MENU:
event.preventDefault();
trace(“display a custom menu”);
break;
case Keyboard.SEARCH:
event.preventDefault();
trace(“perhaps use as a help button”);
break;
}
}

No keyboard event is dispatched for the home soft key.

Keep in mind that overriding the back button default behavior is against Android UX guidelines. Some users may give your application a bad review because of it.

A trick I often use is to override these buttons during development to test various cases at runtime. I may, for instance, use the search button to increment a variable, add a listener, or display a benchmarking tool.

Exit mobile version