Android NDK: How to handle back key press in Cocos2dx?

The only question is what should I say about this. I need to know how to handle special keystrokes such as back, menuetc. In android NDK. I use Cocos2dX, so if you could give me Cocos2dX's special answer, that would be great.

+5
source share
4 answers

In cocos2dx, everyone CCLayergets the following methods, which can be overridden to add functionality to them:

class CC_DLL CCKeypadDelegate
{
public:
    // The back key clicked
    virtual void keyBackClicked() {}

    // The menu key clicked. only avialble on wophone & android
    virtual void keyMenuClicked() {};
};

CCLayerinherited from CCKeypadDelegate. And each screen can give an implementation to these functions.

+7
source

In Cocos2d-x, you have to do this to implement

 virtual void keyBackClicked();

as well as this

 this->setKeyPadEnable(true);

in .cpp class

+1

: this->setKeypadEnabled(true);

onKeyReleased:   virtual void onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event);

keyCode:

void GameScene::onKeyReleased(EventKeyboard::KeyCode keyCode, cocos2d::Event *event)
{
    if (keyCode == EventKeyboard::KeyCode::KEY_BACKSPACE) {
        CCLOG("You pressed back button");
    }
    else if(keyCode == EventKeyboard::KeyCode::KEY_MENU)
    {
        CCLOG("You pressed menu button");
    }
}
+1

If your class is not inherited with CClayer, you can handle this through calls JNI. In Cocos2dxGLSurfaceView.javayou handle back and the menu key of the onKeyDownfunction

public boolean onKeyDown(final int pKeyCode, final KeyEvent pKeyEvent)
0
source

All Articles