Home > ActionScript 3, AIR 2.5, Froyo > AIR on Android: Handling the BACK and MENU Hardware Buttons

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
  1. Ed
    July 21, 2011 at 23:24 | #1

    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. :-)

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.