MAGS94
MAGS94

Reputation: 510

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

Upvotes: 2

Views: 401

Answers (2)

Tenfour04
Tenfour04

Reputation: 93902

As @Xoppa says, you're using false when you should be using true.

Also, it doesn't really make sense to call this code in response to a button being pressed, because it's a setting for how to respond to buttons. In fact, keyDown will never be called for the back button, since Libdgx isn't catching it. You should call it only one time, in create().

Upvotes: 1

Xoppa
Xoppa

Reputation: 8123

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.

Upvotes: 2

Related Questions