2

Is there a simple way to handle key inputs from within the layer? I know with touch inputs you can just use ccTouchesEnded. Is there a similar function for keyPressed? If not how would you go about getting the key press?

CodeSmile
  • 64,284
  • 20
  • 132
  • 217

1 Answers1

2

I start working with cocos2d two days ago, and I had the same question.

I found a way to detect key event into game.

You have to enable key event detection in your game class

// Enable keys
this.setIsKeyEnabled(true);

and override

@Override
public boolean ccKeyDown(int keyCode, KeyEvent event) {

    Log.d("ccKeyDown", "Entered");

    return super.ccKeyDown(keyCode, event);
}

In you Android activity override on KeyDown and dispatch event to game class

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    Log.d("Activity", "key entered");

    return CCDirector.sharedDirector().onKeyDown(event);

}

Hope that helps you :) Or you found solution...Its been a long time xD cheers

Veljko
  • 1,893
  • 6
  • 28
  • 58