Search code examples
javaandroidinputlibgdx

Back Key in Libgdx


I want my game not to respond to back key on menu screen if player wants to exit so he has to click exit button to exit game

My class MenuScreen implements GestureListener, InputProcessor and has stage as I want to use fling() of GestureListener and keyDown() of InputProcessor

so I do this

multiplexer = new InputMultiplexer(stage, new GestureDetector(this), this); Gdx.input.setInputProcessor(multiplexer); Gdx.input.setCatchBackKey(false);

but this does nothing

I also tried to do this in keyDown() instead of show()

    @Override
        public boolean keyDown(int keycode)
    {
    if(keycode == Input.Keys.BACK)
        Gdx.input.setCatchBackKey(false);
return true;
    }

but also nothing happened


Solution

  • If you want your application to catch the back key instead of the OS, then you want to use true instead of false as argument of the setCatchBackKey method. See also the documentation:

    Sets whether the BACK button on Android should be caught. This will prevent the app from being paused. Will have no effect on the desktop.