Hi,
I started looking at the library source last night, and realized that there doesn’t seem to be a way to get generic pc keyboard keypresses reported to an application. Since cocos2d-x now supports desktop platforms, I think it would be great if it supported real keyboard input. I will propose a solution below, but as I am a total newbie at this, please bear in mind that I may have missed something important… I’m working on the Mac platform.
1. Simple keyboard support exists in the keypad_dispatcher/* files. CCKeypadDelegate needs to be extended to look something like this:
class CC_DLL CCKeypadDelegate
{
public:
virtual void keyBackClicked() {}
virtual void keyMenuClicked() {};
virtual void keyDown(unsigned short keyCode) {};
virtual void keyUp(unsigned short keyCode) {};
};
2. On the Mac platform, keyboard events seem to originate in platform/mac/EAGLView, in the keyUp / keyDown methods. keyUp looks like this:
- (void)keyUp:(NSEvent **)theEvent
{
DISPATCH_EVENT;
;
}
Key events don’t seem to work for me at all . However, they do work if I do this:
cocos2d::CCDirector::sharedDirector()->getKeypadDispatcher()->dispatchKeypadMSG(cocos2d::kTypeMenuClicked);
in the method. I’m not sure how the method is supposed to work; perhaps there is a problem with the DISPATCH_EVENT macro? I haven’t had time to look into it yet.
**3.* To report keypresses, CCKeypadDispatcher::dispatchKeypadMSG needs to somehow accommodate key codes. I propose the following signature change, but I realize that this is not compatible with cocos2d-iphone, and therefore will not be accepted in this form:
bool CCKeypadDispatcher::dispatchKeypadMSG(ccKeypadMSGType nMsgType, unsigned short keyCode = 0)
4. Finally, perhaps we could extend ccKeypadMsgType to:
typedef enum { kTypeBackClicked = 1, kTypeMenuClicked, kTypeKeyUp, kTypeKeyDown, } ccKeypadMSGType;
Sorry if this seems a bit incoherent, I’m kind of tired after staring at unfamiliar code for too long. It’d be interesting to hear from you experienced guys if you think anything along these lines would be possible. Otherwise, I will have to use a custom build of cocos2d-x for my game.
Thanks in advance!