Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

share|improve this question

2 Answers 2

up vote 3 down vote accepted

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.
share|improve this answer

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);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.