1
\$\begingroup\$

I've added an InputListener to my libgdx actor and I've added the actor to the stage. The stage (and all of its actors) gets drawn just fine.

The problem is my actor doesn't seem to be receiving keyboard inputs as nothing happens when I press keys.

@Override
public void show() {
 stage = new Stage();
 player = new Player();
 player.setPosition(600, 600);
 stage.setKeyboardFocus(player);
 player.addListener(new InputListener(){
   @Override
   public boolean keyDown(InputEvent event, int keyCode) {
    System.out.println("Key Down");
    return true;
   }
 });
 stage.addActor(player);
}
\$\endgroup\$
1
\$\begingroup\$

The problem was I hadn't set the stage as the InputProcessor, so I did that onShow():

@Override
public void show(){
 ...
 Gdx.input.setInputProcessor(stage);
}

I also set it to null onHide() just in case:

@Override
public void hide(){
 ...
 Gdx.input.setInputProcessor(null);
}
\$\endgroup\$
  • \$\begingroup\$ I can confirm that it has to be done in show(). I tried to do it in my Screen class's constructor and wondered why it didn't work. Anyway, thanks! \$\endgroup\$ – Aron Lorincz May 17 '14 at 8:26
0
\$\begingroup\$

Notice

int keyCode

Replace it with the key code of the key you want it to listen for.

\$\endgroup\$
  • \$\begingroup\$ I assumed keyDown() was meant to be used to catch keyboard inputs regardless of what key was being pressed? \$\endgroup\$ – 0x6C38 Oct 10 '13 at 15:03
  • \$\begingroup\$ Well I've not used libgdx, but it wouldn't accept a keyCode as a parameter if it wasn't meant for specific keys. I don't know how you'd get it to check for any un-specified key event though. \$\endgroup\$ – Troubleshoot Oct 10 '13 at 15:08

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.