user1010381
user1010381

Reputation: 21

How to handle key input with cocos2D in Android?

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?

Upvotes: 2

Views: 966

Answers (1)

Veljko
Veljko

Reputation: 1903

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

Upvotes: 2

Related Questions