Add the Enter key and arrow key response in the cocos2d-x

Source: Internet
Author: User

Recently, a set-top box project requires the direction keys of the remote control to control the direction in the game.

For general Android devices, iOS devices, or Win32 platforms, the cocos2d-x provides the "ccaccelerometer" class that can be used to control the game UI. However, because the set-top box uses the remote control to interact with players while the gsenser is missing in the remote control, you can only use the direction keys and keys temporarily. Fortunately, the set-top box manufacturer uses the android standard key value. The direction keys and keys correspond to the andriod DPAD keys one by one.

Next I will explain how to modify the cocos2d-x so that we can perceive DPAD key messages in the game.

Modify cocos2dx \ keypad_dispatcher \ cckeypaddelegate. h

Add two virtual functions for "cckeypaddelegate:

class CC_DLL CCKeypadDelegate{public:    // The back key clicked    virtual void keyBackClicked() {}    // The menu key clicked. only available on wophone & android    virtual void keyMenuClicked() {};    // The enter key clicked. only available on win32 & android    virtual void keyEnterClicked() {};    // The arrow key clicked. only available on win32 & android    virtual void keyArrowClicked(int arrow) {};};

The "keyenterclicked" function is used to respond to the "enter" key, and the "keyarrowclicked" function is used to respond to the direction key message.

Cocos2dx \ keypad_dispatcher \ cckeypaddispatcher. h

Modify the parameter eypadmsgtype to the following:

typedef enum {    // the back key clicked msg    kTypeBackClicked = 1,    // the menu key clicked msg    kTypeMenuClicked,    // the Enter key clicked msg    kTypeEnterClicked,    // the arrow key clicked msg    kTypeLeftArrowClicked,    kTypeUpArrowClicked,    kTypeRightArrowClicked,    kTypeDownArrowClicked,} ccKeypadMSGType;

Cocos2dx \ keypad_dispatcher \ cckeypaddispatcher. cpp
Modify the "dispatchkeypadmsg" function, in:

case kTypeMenuClicked:                pDelegate->keyMenuClicked();

Add:

case kTypeEnterClicked:pDelegate->keyEnterClicked();break;case kTypeLeftArrowClicked:case kTypeUpArrowClicked:case kTypeRightArrowClicked:case kTypeDownArrowClicked:pDelegate->keyArrowClicked(nMsgType);               break;

For the Android platform, you also need to modify the following:

ThuCocos2dx \ platform \ Android \ Java \ SRC \ org \ cocos2dx \ Lib \ cocos2dxglsurfaceview. Java
Find the onkeydown function, and add several key-value processing in it, so that the function is as follows:

@Overridepublic boolean onKeyDown(final int pKeyCode, final KeyEvent pKeyEvent) {switch (pKeyCode) {case KeyEvent.KEYCODE_BACK:case KeyEvent.KEYCODE_MENU:case KeyEvent.KEYCODE_DPAD_UP:// 19case KeyEvent.KEYCODE_DPAD_DOWN:// 20case KeyEvent.KEYCODE_DPAD_LEFT:// 21case KeyEvent.KEYCODE_DPAD_RIGHT:// 22case KeyEvent.KEYCODE_DPAD_CENTER:// 23this.queueEvent(new Runnable() {@Overridepublic void run() {Cocos2dxGLSurfaceView.this.mCocos2dxRenderer.handleKeyDown(pKeyCode);}});return true;default:return super.onKeyDown(pKeyCode, pKeyEvent);}}

* Note:: You must modify proj. Android \ SRC \ org \ cocos2dx \ Lib \ cocos2dxglsurfaceview. Java in the project directory.

5cocos2dx \ platform \ Android \ JNI \ touchesjni. cpp

Find:

    #define KEYCODE_BACK 0x04    #define KEYCODE_MENU 0x52

Add the following content:

#define KEYCODE_DPAD_UP19    #define KEYCODE_DPAD_DOWN20    #define KEYCODE_DPAD_LEFT21    #define KEYCODE_DPAD_RIGHT22    #define KEYCODE_DPAD_CENTER23

Then modify the "java_org_cocos2dx_lib_cocos2dxrenderer_nativekeydown" function, as follows:

    JNIEXPORT jboolean JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeKeyDown(JNIEnv * env, jobject thiz, jint keyCode) {        CCDirector* pDirector = CCDirector::sharedDirector();        switch (keyCode) {            case KEYCODE_BACK:                  if (pDirector->getKeypadDispatcher()->dispatchKeypadMSG(kTypeBackClicked))                    return JNI_TRUE;                break;            case KEYCODE_MENU:                if (pDirector->getKeypadDispatcher()->dispatchKeypadMSG(kTypeMenuClicked))                    return JNI_TRUE;                break;                  case KEYCODE_DPAD_UP:                if (pDirector->getKeypadDispatcher()->dispatchKeypadMSG(kTypeUpArrowClicked))                    return JNI_TRUE;                break;            case KEYCODE_DPAD_DOWN:                if (pDirector->getKeypadDispatcher()->dispatchKeypadMSG(kTypeDownArrowClicked))                    return JNI_TRUE;                break;            case KEYCODE_DPAD_LEFT:                if (pDirector->getKeypadDispatcher()->dispatchKeypadMSG(kTypeLeftArrowClicked))                    return JNI_TRUE;                break;            case KEYCODE_DPAD_RIGHT:                if (pDirector->getKeypadDispatcher()->dispatchKeypadMSG(kTypeRightArrowClicked))                    return JNI_TRUE;                break;                 case KEYCODE_DPAD_CENTER:                if (pDirector->getKeypadDispatcher()->dispatchKeypadMSG(kTypeEnterClicked))                    return JNI_TRUE;                break;                 default:                return JNI_FALSE;        }        return JNI_FALSE;    }

Now the Android platform has been modified.
To facilitate our development in vs2010, we also modify the Win32 platform:
Six cocos2dx \ platform \ Win32 \ cceglview. cpp

Find the "windowproc" function and find:

if (wParam == VK_F1 || wParam == VK_F2)        {            CCDirector* pDirector = CCDirector::sharedDirector();            if (GetKeyState(VK_LSHIFT) < 0 ||  GetKeyState(VK_RSHIFT) < 0 || GetKeyState(VK_SHIFT) < 0)                pDirector->getKeypadDispatcher()->dispatchKeypadMSG(wParam == VK_F1 ? kTypeBackClicked : kTypeMenuClicked);        }        else if (wParam == VK_ESCAPE)        {            CCDirector::sharedDirector()->getKeypadDispatcher()->dispatchKeypadMSG(kTypeBackClicked);        }

Then add the following:

else if (wParam == VK_RETURN){CCDirector::sharedDirector()->getKeypadDispatcher()->dispatchKeypadMSG(kTypeEnterClicked);}else if (wParam >= VK_LEFT && wParam <= VK_DOWN){CCDirector::sharedDirector()->getKeypadDispatcher()->dispatchKeypadMSG((ccKeypadMSGType)(kTypeLeftArrowClicked + wParam - VK_LEFT));}

Now that the Win32 platform has been added, the program can respond to the direction keys and return keys on the keyboard.

The usage is as follows:
Suppose we want to add two functions in the header file of helloworld inherited from cclayer:

void keyArrowClicked(int arrow);void keyEnterClicked();

Then, enable the key function during helloworld initialization:

setKeypadEnabled(true);

Implement the two previously declared virtual functions in helloworld:

Void llplanecontrollayer: keyarrowclicked (INT arrow) {If (arrow = ktypeleftarrowclicked) {// press the left arrow key} else if (arrow = ktyperightarrowclicked) {// right arrow key press} If (arrow = ktypeuparrowclicked) {// top arrow key press} else if (arrow = ktypedownarrowclicked) {// bottom arrow key press} void llplanecontrollayer:: keyenterclicked () {// press the Enter key}

When you press the arrow key, keyarrowclicked is executed, and the arrow parameter indicates the key to be pressed. When you press enter (dpad_center for Android), The keyenterclicked function is executed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.