简体   繁体   中英

Disabling the phone's back button (AIR for Android /ActionScript 3)

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!

@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;

}
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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM