0

I'm making a game for Android using AIR (meaning it's programmed in ActionScript 3, same as Flash).

What I'd like to do is to make the physical back button on the phone NOT exit the game, it should pause the game instead. (I will make it so that it will still exit the game if pressed twice rapidly.)

However my code is not working:

public function Main() {
    if (Capabilities.cpuArchitecture=="ARM") {
        NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN, onMainKeyDown);
    }
}
private function onMainKeyDown(ke:KeyboardEvent) {
    if (ke.keyCode==Keyboard.BACK) {
        // Pause the game here.
        ke.preventDefault();
        ke.stopImmediatePropagation();
    }
}

When I publish the thing to my device it still exits when I'm pressing the physical back button on the phone.

What am I doing wrong here?

Edit: There was just a null pointer exception issue I hadn't discovered yet. How embarrassing!

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Peter
  • 1
  • 1
  • 3

2 Answers2

0
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) { 
          //Here you can do what ever you want to do while pressing the back button
       }
       return true;

}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Abhijit Chakra
  • 3,201
  • 37
  • 66
0
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. 
   }
}

Note that if you’ve set stage.displayState = FULL_SCREEN, no keyboard events are sent to your app! Use stage.displayState = FULL_SCREEN_INTERACTIVE instead!

Panoman
  • 387
  • 3
  • 7