3
\$\begingroup\$

How to handle a event for both key as well as actor added in the screen? My problem is that at a time only one input processor is available either for button or device back button. How can I set input processor for both back button of device and button which act as actor on the screen.

I implemented following code for button input processor Gdx.input.setInputProcessor(stage); button was added in the stage so I implement input processor for the stage.

For device back button Gdx.input.setInputProcessor(this); Register actor as input processor

In keyDown event I write if (keycode == Keys.ESCAPE || keycode == Keys.BACK) LevelLoadScreen.flagResume = true;

Other solution also acceptable if it handle both the action listener

\$\endgroup\$

2 Answers 2

4
\$\begingroup\$

Not sure if I understand your question clearly. I'll assume the following:

  • You got a Stage set as InputProcessor
  • You got another special button as InputProcessor which you refer to as "device back button"
  • You want them both to process input but it seems you can only set 1 InputProcessor at a time

If above is the case, then one way is to use an InputMultiplexer which is apart of libgdx.

First, create an InputMultiplexer.

Then, set it as the InputProccessor.

Add your two InputProcessors to the InputMultiplexer: back button, then stage in this order.

  • If you return false in back button event handlers, then the event gets passed to stage as well.
  • If you return true in back button event handlers, then events do not get raised in stage.
\$\endgroup\$
3
\$\begingroup\$

You have to overwrite the keyDown function in Stage.

See this example code

mStage = new Stage() {
        @Override
        public boolean keyDown(int keyCode) {
            if (keyCode == Keys.BACK) {
                Gdx.app.exit();
            }
            return super.keyDown(keyCode);
        }
    };
    Gdx.input.setInputProcessor(mStage);
    Gdx.input.setCatchBackKey(true);
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .