PC keyboard support?

Hi, I was searching for cocos2d-x PC keyboard implementation and I found nothing.
There is really no implementation of such a basic thing out there?
And how is it possible that framework itself does not cointain support for HW keyboard?
It would be really great if someone could share any code solving this problem.

Thanks in advance :slight_smile:

im also looking for kind of the same thing but more or less cross platform keyboard support

(something that magically works on all platforms and gives you keydown events)

Iā€™m currently using a quick and dirty hack for keyboard support for Windows and OS X.

In the CCDirector class I have booleans for when keys are down or have been pressed:

CCDirector.h: http://pastebin.com/BviXS0JV
CCDirector.cpp: http://pastebin.com/Ts9WDD2R

And then I modify those booleans in each platform specific code file for event listening.

For OS X
CCEventDispatcher.mm: http://pastebin.com/x4XAJvcB

and win32
CCEGLView.cpp http://pastebin.com/asAf7qSF

There has been talk about adding proper keyboard support but Iā€™m not sure how far away that is.

You solution is really dirty like you sad :smiley:

So I have implemented keyboard support throught glfw (should be multiplatform) and integrated into the sources with tests.

https://github.com/simpliplant/cocos2d-x

Usage:
call ā€œsetKeyboardEnabled(true)ā€ in your Layer derived class and you define methods ā€œkeyPressed(int keyCode)ā€ and ā€œkeyReleased(int keyCode)ā€, see KeyboardTest in samples/Cpp/TestCpp/Classes

Problem is that I am only able to test it out on Linux so I edided only these tests.
Can someone take the code from my forked repo and test it out on OSX or Win and maybe correct mac and win32 tests? :slight_smile:

Works for OSX. I just modified CCEventDispatcher.mm (http://pastebin.com/KTsGvShH) and then added KeyboardTest.h and KeyboardTest.cpp in the TestCpp xcode project. Adding it to win32 should be pretty straightforward as well.

I do get that annoying bell sound each time I press a key though. I know there is a way to disable it but Iā€™ve forgotten. I think it had something to do with not calling [self super] somewhere. Iā€™ll update this post if I find out how.

Also Iā€™m just wondering what this code does in CCEventDispatcher.mm?

@
DL_FOREACH_SAFE( keyboardDelegates_, entry, tmp ) {
if ( entry->flags & kCCImplementsKeyDown ) {
void *swallows = [entry->delegate performSelector:selector(ccKeyDown:) withObject:event]; if( swallows ) break; } }

It almost looks like it already has keyboard supportā€¦ Iā€™ve probably missed something.

For that annoying bell sound on OSX, you can disable it by commenting out line 453 in cocos2dx/platform/mac/EAGLView.mm.

@
// pass the event along to the next responder (like your NSWindow subclass)
//[super keyDown:theEvent];
@

Please let me know if you find a better solution.

RenĆ© Klačan wrote:

https://github.com/simpliplant/cocos2d-x
>
Usage:
call ā€œsetKeyboardEnabled(true)ā€ in your Layer derived class and you define methods ā€œkeyPressed(int keyCode)ā€ and ā€œkeyReleased(int keyCode)ā€, see KeyboardTest in samples/Cpp/TestCpp/Classes

This is great, works nice on linux. Iā€™ll try to test windows soon. Is there some documentation about keycodes? For some reason googling doesnā€™t provide me with much on ā€œglfw key codesā€. I would like a list of constants.

Michael Chinen wrote:

This is great, works nice on linux. Iā€™ll try to test windows soon. Is there some documentation about keycodes? For some reason googling doesnā€™t provide me with much on ā€œglfw key codesā€. I would like a list of constants.

Iā€™m glad you like it.

Constants Iā€™m using in my project - https://github.com/simpliplant/cocos2d-x/wiki/Keyboard-keycodes-enum :slight_smile:

RenĆ© Klačan wrote:

Iā€™m glad you like it.
>
Constants Iā€™m using in my project - https://github.com/simpliplant/cocos2d-x/wiki/Keyboard-keycodes-enum :slight_smile:

I added it to mac using the snippets mentioned by Rohin Knight.

I also sent you a pull request if you want to take it on/so others can access it.

If I do the windows one, Iā€™ll push that to you as well.
Mac users will still need to add the -DKEYBOARD_SUPPORT to their xcode projectā€™s C files. Hopefully this will go away if it is pulled into upstream master.

Unfortunately, the glfw keycodes are different for mac and linux. I get 0-3 for asdf on mac instead of the ascii values I get on linux.

Any chances this will get merged upstream?

Iā€™m using cocos2d as a git submodule, not sure how I use your fork easily (without removing the submodule and adding yours).

Diego Ponciano wrote:

Any chances this will get merged upstream?
>
Iā€™m using cocos2d as a git submodule, not sure how I use your fork easily (without removing the submodule and adding yours).

It was merged into the mainstream develop branch few minutes ago.

@RenĆ© Klačan
Thank you
Now I will be able to test games on linux, thatā€™s great!

So which branch is the keyboard support in now?

The main cocos2dx/cocos2dx or someone elseā€™s?

Itā€™s in master under the main cocos2d / cocos2d-x.

Also hereā€™s the test for it: https://github.com/cocos2d/cocos2d-x/tree/develop/samples/Cpp/TestCpp/Classes/KeyboardTest

Brandon E wrote:

So which branch is the keyboard support in now?
>
The main cocos2dx/cocos2dx or someone elseā€™s?

Actually it is in main repo but in develop branch, but also in my dev and keyboard_support branches of cocos2d-x on my github

This keyboard support is great news!

But Iā€™m having problems getting the keyboard to work. Where am I suppose to define the macro SUPPORT_KEYBOARD? I understood correctly it needs the KEYBOARD_SUPPORT macro to build properly. But if I try to define it in ccMacros.h it doesnā€™t work. I also tried to put it in Visual Studios preprocessor definitions but it still wont compile. What am I doing wrong?

It gives me this error:
Error 5 error LNK2001: unresolved external symbol ā€œpublic: virtual bool __thiscall cocos2d::Layer::isKeyboardEnabled(void)ā€

Iā€™m a novice with Visual Studio, cocos2d-x and C++ so is there something really basic Iā€™m not understanding correctly?

Thanks

Antti P wrote:

This keyboard support is great news!
>
But Iā€™m having problems getting the keyboard to work. Where am I suppose to define the macro SUPPORT_KEYBOARD? I understood correctly it needs the KEYBOARD_SUPPORT macro to build properly. But if I try to define it in ccMacros.h it doesnā€™t work. I also tried to put it in Visual Studios preprocessor definitions but it still wont compile. What am I doing wrong?
>
It gives me this error:
Error 5 error LNK2001: unresolved external symbol ā€œpublic: virtual bool __thiscall cocos2d::Layer::isKeyboardEnabled(void)ā€
>
Iā€™m a novice with Visual Studio, cocos2d-x and C*+ so is there something really basic Iā€™m not understanding correctly?
You are getting this error because the cocos2d dlls you built didnā€™t have the SUPPORT_KEYBOARD definition when you built them, but your own cocos2d-x app/project did.
You can add SUPPORT_KEYBOARD to the cocos2d-x project by right clicking the libcocos2dx project~~>config properties~~>c/c*Ā±>preprocessor and adding it there (note that the semicolons separate the symbols).

It should look like the image here (the question is unrelated):

Then rebuild cocos2d-x libs (clean the project and rebuild it however you did before), and cross your fingers.

Hi Antti,

except settings KEYBOARD_SUPPORT directive (Michael posted a nice explanation how) there is also problem that merged code supports just Linux and Mac but please take a look at my ā€œwindows_keyboard_supportā€ branch in my cocos2d-x repo (https://github.com/simpliplant/cocos2d-x/tree/windows_keyboard_support) where I updated code so it should make a keyboard work for a windows (I didnt test it so problems can occur).

Hi RenƩ,

Tried your branch with Win32 keyboard support, and it works great, thanks! Iā€™m running Windows 8, compiling on VS 2012 Express.
However, I had to make some changes, one of them being the addition of the CCKeyboardDispatcher files to the cocos2d project. Also:

In CCEGLView.cpp, modified the calls to the keyboard dispatcher to 1 line, like this:

Director::sharedDirector()->getKeyboardDispatcher()->dispatchKeyboardEvent(wParam, true);

In CCLayer.cpp:

Line 303 -> pDirector->getKeyboardDispatcher()->setKeyPressDelegate(nullptr);
Line 304 -> pDirector->getKeyboardDispatcher()->setKeyReleaseDelegate(nullptr);

Finally, in CCKeyboardDispatcher.cpp:

Line 36 -> : _keyPressDelegate(nullptr)
Line 37 -> , _keyReleaseDelegate(nullptr)

Is this correct? It worked on OSX, but I did not test on Linux. Would it be better to implement the delegate the same way the Keypad Dispatcher does?

I also changed the line 527 on CCEventDispatcher.mm (keyPressed() was being called when releasing a key):

kbDisp->dispatchKeyboardEvent(event.keyCode, false);

Thanks again!

Hi Bruno,

thank you very much for your contribution, I have edited files in my repo according to your post.

Yes, it is corrent and your changes does not affect Linux. Originally my implementation was very similar to keypadā€™s implementation but after I have made a pull request back to the cocos2dx repo, cocos2dx guys requested few changes before it will get merged and it ended up this way :slight_smile: