Before I begin, I assume that the reader of this post are familiar with “Cocos2d-x” platform. For those who are not familiar with “Cocos2d-x” platform, I would just say that its cross platform open source 2D game engine framework based on “Cocos2d-iPhone” specific mobile platform. You can further read about “Cocos2d-x” at Cocos2d-x Overview.
Keyboard is one of the most important input mechanism in game development. Since, coco2d-x is based on iphone platform, therefore, its architecture design includes only virtual keypad input and not hardware keyboard input. Cocos2d-x is mainly developed for windows users to develop their games with cocos2d-x platform on windows platform instead of mac. This can help the wide range of windows specific developer community to learn cocos2d platform for mobile game development.
With cocos2d-x on windows platform, its not always easy to use just mouse as a user input. Many people prefer hardware keyboard input but, its a little bit difficult to enable it with cocos2d-x. So, here today, we shall learn “How to enable hardware keyboard with cocos2d-x?“.
There are two methods to enable hardware keyboard with cocos2d-x platform:
1) Enhance Key Dispatcher method.
2) Hook hardware keyboard functions like “ccTouchedBegan or ccTouchesEnded”.
I am going to discuss second method here in this post. You can download detail tutorial or you can follow step by step discussion below. You will also need to download “CCEGLView_win32.cpp” file for second method.
Let us begin now.
Create new project cocos2d-x project and name it “keyboard_tut“.
Edit the “HelloWorldScene.h” file which should looks like below:
We have added “ccTouchesBegan” method, which we shall hook for our hardware keyboard.
add following lines to “init” method in “HelloWorldScene.cpp” file i.e.
What we have done here is add ball sprite, then set is position to windows center and enable touch input to receive inputs to ccTouchesBegan method.
Now, its time for us to hook the hardware keys.
Save your files and close all visual studios instances before you proceed any further.
Go to “~\cocos2dx\platform\win32\” directory inside your cocos2d-x root directory.
Then save “CCEGLView_win32.cpp” file some place else before making any new changes.
Now open “CCEGLView_win32.cpp” file and make following changes after following lines of code:
Add these lines after above code:
What we have done here is hook the ccTouchesBegan & ccTouchesEnded method. You can explore the details about SendMessage Method yourself.
Now run the “build-win32.bat” file provided by the cocos2d-x.
After complete build successful execution, open your visual studio project and add following lines in “HelloWorldScene.cpp” file.
Add following line in “HelloWorldScene.h” file under public methods
What we have done here is that we use GetKeyState method to verify whether hardware key is being pressed or not.
That’s about it. Execute the project and press up and down arrow key you should see that the ball will move up and down accordingly.