libgdx-机器人:拦截返回键和确认退出机器人、libgdx

2023-09-07 08:45:40 作者:胭脂染了紅樓

使用libgdx,我怎么能拦截安卓BACK键,以便做一些preprocessing(如要求确认来自用户),在实际执行命令,退出游戏?

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 exit the game?

推荐答案

1。启用捕获返回键的。

在类实现了ApplicationListener

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

2。处理醒目返回键的。

在实现 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;
   }