2
\$\begingroup\$

Please advice/clarifyon below issue in libgdx while handling back button in Screens.

I have already gone through these 2 threads,

https://stackoverflow.com/questions/7223723/in-libgdx-how-do-i-get-input-from-the-back-button

Is there a way to capture back button twice in same activity libgdx game

As per suggestion in above two threads, I have done something like this in 2 Screen of my Game,

public class MainMenuScreen implements Screen{
    //omitted unnecessary code
    @Override
    public void render(float delta) {
    if (Gdx.input.justTouched()) {
        game.setScreen(game.STS);   //Setting 2nd Screen
    } else if (Gdx.input.isKeyPressed(Keys.BACK)) {
        System.out.println("mms back");
        //if(Gdx.input.isKeyJustPressed(Keys.BACK))
        Gdx.app.exit();            // Exiting   
    }
}

public class StageScreen implements Screen{
    //omitted unnecessary code
    @Override
    public void render(float delta) {
        if (Gdx.input.isKeyPressed(Keys.BACK)) {
        System.out.println("backed to mms in android");
        //if(Gdx.input.isKeyJustPressed(Keys.BACK))
        game.setScreen(game.MMS);  //going back to Main screen
    }
}

Above code works fine, when I press BACK in Stage Screen to call Main Screen, it exits the app immediately, without stopping in my Main screen, which is as per my expectations because I am using Gdx.input.isKeyPressed(Keys.BACK) instead of Gdx.input.isKeyJustPressed(Keys.BACK).

But my problem is, when I use Gdx.input.isKeyJustPressed(Keys.BACK) method in my Screens, it does not (return true) catch BACK key.

I have already set Gdx.input.setCatchBackKey(true) for my Game.

Any idea, why does isKeyJustPressed not responds while isKeyPressed does?

Thanks in Advance.

\$\endgroup\$
2
\$\begingroup\$

After experimenting long time, I came to the conclusion that it is not possible to catch isKeyJustPressed method without implementing input processor. So I have used isKeyPressed method only, without implementing InputProcessor in my Screens.

I have used a boolean flag to get to know that if back button is kept pressed from last screen, it won't exit in first screen untill unless user push the BACK button again.

public boolean backpressed=false;  //defined in Game class

public class MainMenuScreen implements Screen {
    //omitted unnecessary code
    @Override
    public void render(float delta) {
        if (Gdx.input.justTouched()) {
            game.setScreen(game.STS);   //Setting 2nd Screen
        } else if (Gdx.input.isKeyPressed(Keys.BACK)){
            if(!game.backpressed) {
                game.backpressed=true;
            } else if (game.backpressed) {
                game.backpressed=false;
                Gdx.app.exit();
        } 
    }
}

public class StageScreen implements Screen {
    //omitted unnecessary code
    @Override
    public void render(float delta) {
    ...........
    ............
    if (Gdx.input.isKeyPressed(Keys.BACK)) {
        game.backpressed=true;
        game.setScreen(game.MMS);
    }
}
\$\endgroup\$
1
\$\begingroup\$
protected boolean backTo(){
    boolean back = false;

    if(Gdx.input.isKeyPressed(KEY_BACK) && keyBack == false){
        System.out.println("KeyBack: "+Gdx.input.isKeyPressed(Input.Keys.BACK));
        keyBack = true;
        back = false;
    }
    if(Gdx.input.isKeyPressed(KEY_BACK) == false && keyBack){
        keyBack = false;
        back = true;
    }
    return back;
}

/**
 * @keyBack is a boolean variable that I use to handle the state if it
 * still pressing or not... It logics works like this:
 * If we press the back key it will not work because
 * @back value is set to false. However after the key back stop
 * pressing it will check if the
 * @keyBack is true and it will see Oh that's true because we used
 * to say it has to be true while pressing back key. Then it will set
 * @keyBack false then it will can't call this method again until the key
 * be pressed again.
 *
 * @KEY_BACK is a constant I made with the 4 value... It is the value of back.
 *
 */

Here an example how I used this function:

private void menuScene(){
   //Omitted unnecessary code
    if(backTo()){

        if(sceneID == MAIN){
            running = false;
            main.setSceneID(main.TRAVEL);
        } else {
            sceneID = MAIN;
            scene.setRunning(false);
        }
    }
}

In this case after press back my UI delivery the user to a menu and if the user press back again it will delivery to the MainMenu(Load Game and New Game) passing the constant TRAVEL because the MainMenu has also a map where displays where is the user char in currenty slot.

\$\endgroup\$

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.