Disabling the phone's back button (AIR for And

2019-08-16 03:03发布

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!

2条回答
beautiful°
2楼-- · 2019-08-16 03:42
@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;

}
查看更多
爷、活的狠高调
3楼-- · 2019-08-16 03:47
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!

查看更多
登录 后发表回答