Libgdx, how to catch back key, when stage is the input Processor

815 views Asked by At

I have a class called AbstractGameScreen with the following signature:

public abstract class AbstractGameScreen extends InputAdapter implements Screen { ... }

All my screens extend this abstract class. Now the problem i am encountering is that in screens that use stages i already set the stage to be the inputprocessor. But if i do so, the keyUp method doesn't execute on back-key being pressed anymore. if i set the screen to be inputprocessor instead, the stage doesn't work. how do i work around this problem?

public class MenuScreen extends AbstractGameScreen {

 @Override
    public void show() {
        stage = new Stage(new StretchViewport(Constants.VIEWPORT_GUI_WIDTH, Constants.VIEWPORT_GUI_HEIGHT));
        Gdx.input.setInputProcessor(stage);
        Gdx.input.setCatchBackKey(true);
        instructionsVisible= false;
        rebuildStage();
    }

@Override
    public boolean keyUp (int keycode) {
        // Back to Menu
        if (keycode == Input.Keys.ESCAPE || (keycode == Input.Keys.BACK && instructionsVisible)) {
            instructionsVisible=false;
            layerInstruction.setVisible(false);
            layerControls.setVisible(true);
        }
        else
        {
            Gdx.app.exit();
        }
        return false;
    }
1

There are 1 answers

2
John On BEST ANSWER

In this case you need two InputProcessors- the Stage and your AbstractScreen. The way you do this is create a new InputMultiplexer object and configure it something like this:

InputMultiplexer multiplexer = new InputMultiplexer();
multiplexer.addProcessor( stage );
multiplexer.addProcessor( this ); // Your screen
Gdx.input.setInputProcessor( multiplexer );