Keyboard Input - Cocos2d-X 3.0

Hi all,

I’ve been using Cocos2d-X 2.1.4 for a few months now and feel somewhat confident with it. I downloaded the 3.0 Alpha build a couple of days ago after deciding to build a desktop game (OSX/Windows). Keyboard/touch input was easy with 2.1.4, but it appears to be a bit more difficult on 3.0.

Does anyone know how keyboard/mouse input works for Windows/Mac?

I’ve spent 2 days looking around at the samples, tutorials, other forum posts and so on, and have not found an answer. It is getting frustrating as this was a lot easier with the previous Cocos2d-X.

Any help would be appreciated.

Same problem here.

Tried to recompile project made about month ago with today (30 X 2013) develop branch and it won’t go. First problem was that Layer::setKeyboardEnabled() method is no longer here. When I commented those lines it compiled, but nothing hapened when buttons were pressed.

Then I checked keyboard test from project:

And here I found how does it work now. Hope it helps.

Hi Karol, I’ve seen that, and tried to implement it into the project, but it was full of errors, so I didn’t get anywhere.

Can you provide some guidance?

Thanks.

As I see it (but I might be wrong, as I haven’t made a thorough investigation) the key are lines 10-14:

    auto listener = EventListenerKeyboard::create();
    listener->onKeyPressed = CC_CALLBACK_2(KeyboardTest::onKeyPressed, this);
    listener->onKeyReleased = CC_CALLBACK_2(KeyboardTest::onKeyReleased, this);

    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);

You have to make listener that will listen to the keyboard events (first line of above code). Then set methods which it will call, when the button is pressed or released using callbacks (2-3). At the end you give the EventDispatcher pointer to your listener, so it can begin to work (_eventDispatcher member pointer is here and already set, because it is inherited from Node).

PLEASE SOMEONE CORRECT ME HERE:
What the addEventListenerWithSceneGraphPriority(EventListener* listener,Node* node) method I think is doing, is arranging all the listener s from different node s in the order they (the node s) appear on the screen, so they can be called sequentially or they could swallow events preventing them from going any deeper (but I don’t know how, it’s only a guess).
http://www.cocos2d-x.org/reference/native-cpp/V3.0alpha0/d2/d1b/classcocos2d_1_1_event_dispatcher.html#a9137cbc969607bc1c0f539155af16f15

I myself am eager to find the ‘proper’ way of handling keyboard, so give me an feedback if it works for you and maybe we’ll find it :wink:

One last thing I would recommend is not getting used to any type of API that is provided in develop branch, because it will most likely be changed a dozen of times.