AIR on Android: Handling the BACK and MENU Hardware Buttons
Spent a lot of time trying to get this down. The key [no pun intended] in getting this to work is capturing the KEY_DOWN and KEY_UP events and calling preventDefault() and stopImmediatePropagation() on both Events. Code that needs to be executed in place of the default, should be placed in the KEY_UP event.
This was tested on MT4G, Froyo, AIR 2.5.
stage.addEventListener(KeyboardEvent.KEY_DOWN, handleKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, handleKeyUp);
function handleKeyDown(event:KeyboardEvent):void
{
switch(event.keyCode)
{
case Keyboard.BACK:
event.preventDefault();
event.stopImmediatePropagation();
break;
case Keyboard.MENU:
event.preventDefault();
event.stopImmediatePropagation();
break;
default:
break;
}
}
function handleKeyUp(event:KeyboardEvent):void
{
switch(event.keyCode)
{
case Keyboard.BACK:
event.preventDefault();
event.stopImmediatePropagation();
break;
case Keyboard.MENU:
event.preventDefault();
event.stopImmediatePropagation();
break;
default:
break;
}
}
Cheers!
Advertisement
Categories: ActionScript 3, AIR 2.5, Froyo
THANK YOU!!!! Been looking for an answer to this for the past few nights. Tried everything under the sun but this was the one that did it.