PC keyboard support?


#1

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:


[3.0 - Keyboard] - KeyDown / KeyUp function
#2

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)


#3

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.


#4

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.

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:


#5

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.


#6

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.


#7

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.


#8

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:


#9

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.


#10

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).


#11

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.


#12

@René Klačan
Thank you
Now I will be able to test games on linux, that’s great!


#13

So which branch is the keyboard support in now?

The main cocos2dx/cocos2dx or someone else’s?


#14

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


#15

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


#16

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


#17

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.


#18

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).


#19

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!


#20

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: