Cocos2d-x 2.x keyboard implementation for Mac OS X

In case someone has problems implementing Mac Cocos2d-x 2.x keyboard like I did, here is link to GitHub with working solution: https://github.com/joni-mikkola/cocos2d-x/tree/keyboard

I have provided sample project with the implementation code. I downloaded the original code from somewhere in Cocos2d-x forums and modified it properly to work with Mac OS X. All you have to do, is to add files from cocos2dx/keyboard_dispatcher to your XCode project, then put following in your CCLayer inheriting class:

class HelloWorld : public cocos2d::CCLayer, public cocos2d::CCKeyboardDelegate {
...
virtual void keyDown(int key);
virtual void keyUp(int key);
}

bool HelloWorld::init() {
...
CCDirector::sharedDirector()->getKeyboardDispatcher()->addDelegate(this);
}

void HelloWorld::keyDown(int key) {
CCLog("%d pressed", key);
}

void HelloWorld::keyUp(int key) {
CCLog("%d released", key);
}

If you are having some sort of difficulties with the code, please leave a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *

Begin typing your search term above and press enter to search. Press ESC to cancel.