Jack
Jack

Reputation: 11

AS3: Air For Android - Using hardware back key to navigate to last used scene

I have been searching around to find a generic way to use the back key on android devices to go back to the previous scene you were on. All I seem to find is how to make it so the button does not close the application.

Here is my current code:

NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown, false, 0, true)

function onKeyDown(event:KeyboardEvent):void
{
  if( event.keyCode == Keyboard.BACK )
  {
    event.preventDefault();
    event.stopImmediatePropagation();
    //handle the button press here.

  }
}

Upvotes: 0

Views: 345

Answers (1)

Mr. Phantom
Mr. Phantom

Reputation: 517

Just tested it on my Android device, just change trace() with your function:

stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);  

private function onKeyPressed(event:KeyboardEvent):void 
{
    if(event.keyCode == Keyboard.BACK)
    {
         event.preventDefault();
         //Now you can call your function.
         trace("Hello World");
    }
}

Upvotes: 1

Related Questions