0

After I set up my new project with libGDX v1.3.1 i am stuck with something that should be easy. In my main class which extends game I have:

Gdx.input.setCatchBackKey(true); 

but I cannot get any response from this (in render method):

if(Gdx.input.isButtonPressed(Keys.BACK)){
    this.dispose();
}

It like nothing was pressed, although I can see that the button was pressed in logCat console.

I need to mention that I was using the exact same code until libGDX version 1.0.0 (or the first one with gradle).

Note: i have also tried implementing InputProcessor and then setting the input processor. Result was the same.

And for the home button: using Gdx.input.setCatchMenuKey(true); is not working.

P.T.
  • 24,557
  • 7
  • 64
  • 95
raco
  • 398
  • 1
  • 5
  • 18
  • Just to confirm: 1) this same code worked for you with libgdx 1.0? and 2) you are sure that "this.dispose()" is not being invoked (have you put a log statement in the `isButtonPressed` case?). See http://stackoverflow.com/questions/15501703/how-to-pause-and-resume-in-libgdx-using-the-back-key (the order for `setInputProcessor` and `setCatchBackKey` is important. – P.T. Sep 03 '14 at 03:20
  • 1. yes this did work (in old version without gradle nd in first one with gradle). 2. There is not a problem with this.dispose(); because if(Gdx.input.isButtonPressed(Keys.BACK)) is never activated. I have put System.out.println("working"); in it just be be sure if method ghets activated. But nothing. 3. it doesnt matter id i set imput processor or not. if(keycode == Keys.BACK){ or if(Gdx.input.isButtonPressed(Keys.BACK)){ are never invoked. – raco Sep 03 '14 at 10:32

1 Answers1

2

Use isKeyPressed instead of isButtonPressed. Buttons only refers to the three mouse buttons on a desktop game. Everything on Android is a Key (or Peripheral).

You mentioned "home button" but the code you posted is for the menu button. There is no way to catch the home button unless you make your manifest declare your app as a launcher replacement, in which case, the home button will always open your app, even when it's closed, and the user will have no easy way to get to the home screen. And libgdx doesn't have that functionality built in, since that would be weird. You would have to implement it yourself in your manifest and main Activity.

Also, disposing of this, whatever this is, sounds dangerous to do from the input handler. You could be about to render stuff and cause a crash. But I'm not sure where you're trying to use it from. Maybe it's OK.

Tenfour04
  • 83,111
  • 11
  • 94
  • 154
  • That was exactly what i did. I feel so stupid right now. Thank you And for this.dispose(): i have just tryed anything that i could remember so it was just for testing puropses. – raco Sep 03 '14 at 21:30