raco
raco

Reputation: 408

Catching back and home keys

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.

Upvotes: 0

Views: 532

Answers (1)

Tenfour04
Tenfour04

Reputation: 93581

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.

Upvotes: 2

Related Questions