libgdx-android: Intercepting back key and confirm exit

后端 未结 1 625
[愿得一人]
[愿得一人] 2021-01-01 20:35

Using libgdx, how can I intercept the android BACK key in order to do some preprocessing (e.g. asking for confirmation from user), before actually performing the command to

1条回答
  •  时光说笑
    2021-01-01 21:27

    1. Enable catching of Back Key.

    In the class that implements ApplicationListener

       @Override
       public void create() {
            ...
            Gdx.input.setCatchBackKey(true);
            ...
       }
    

    2. Handle catching of Back Key.

    In a class that implements the InputProcessor

       @Override
       public boolean keyDown(int keycode) {
            ...
            if(keycode == Keys.BACK){
               // Optional back button handling (e.g. ask for confirmation)
               ...
               if (shouldReallyQuit)
                 Gdx.app.exit();
            }
            return false;
       }
    

    0 讨论(0)
提交回复
热议问题