PC keyboard support

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. :wink: 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!

Johnny Andersson wrote:

class CC_DLL CCKeypadDelegate
{
public:
virtual void keyBackClicked() {}
virtual void keyMenuClicked() {};
virtual void keyDown(unsigned short keyCode) {};
virtual void keyUp(unsigned short keyCode) {};
};

These won’t get invoked (even if you implemented them in the layer) unless the layer invokes *CCLayer::setKeypadEnabled( bool )* and pass true to it.

Thanks for the reminder. What do you think about the approach in general - does it seem reasonable to extend CCKeypadDelegate in this way, or should keystrokes be implemented separately in a parallel delegate? (After all, a keyboard isn’t really a keypad).

I will have a look at the implementation in the ObjC library (which, if I understand the post at http://www.cocos2d-iphone.org/forum/topic/11725 correctly, has this functionality) and compare it to the C++ one as soon as I have some time to spare. If anyone knows that that will be futile, or knows a better solution, or that I’m barking up the wrong tree completely, now would be a good time to tell me. :wink:

/J

For the record: I solved this by creating an object implementing the CCKeyboardEventDelegate protocol and then doing
[[CCEventDispatcher sharedDispatcher] addKeyboardDelegate:myDelegate priority:0];

Much easier than modifying the library… :wink: although I still feel that there should be a C+±only way to do this.

/J

Hi Johnny,

Do you have example source code on how you implemented CCKeyboardEventDeletgate and where you placed the add method?

Thanks.