1
\$\begingroup\$

I'm trying to use the method Gdx.input.isKeyJustPressed() to check if the Android BACK button was just pressed. I don't want to use the Gdx.input.isKeyPressed method to avoid multiple calls. Here is my code:

public void handleInput() {
    if(Gdx.input.isKeyJustPressed(Input.Keys.BACK)) {
         // show previous screen...
    }
}

public void update(float dt) {
    handleInput();
}

But unfortunately this doesn't work. Any ideas?

\$\endgroup\$
  • \$\begingroup\$ This does work. Put a breakpoint inside handleInput() and see if the code is even reached, it's probably not and your missing a call to it somewhere deeper down the line. \$\endgroup\$ – Madmenyo Jul 21 '15 at 15:22
  • \$\begingroup\$ @MennoGouw If I replace isKeyJustPressed() with isKeyPressed() it's working. The problem seems to be with the isKeyJustPressed() method. \$\endgroup\$ – Andreas Zock Jul 21 '15 at 16:13
1
\$\begingroup\$

Below code works for me, I get the message when pressing the back button.

if (Gdx.input.isKeyJustPressed(Input.Keys.BACK))
        {
            Gdx.app.log("Debug", "Back pressed!");
        }

Are you calling below method before the one above?

    Gdx.input.setCatchBackKey(true);
\$\endgroup\$
  • \$\begingroup\$ I also called the setCatchBackKey(true). But it it doesn't work. I'm going to use the isKeyPressed() method and prevent multiple calls by saving the last key state and make only a call when the last key state is different from the current. \$\endgroup\$ – Andreas Zock Jul 21 '15 at 21:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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