3

I want to track android back button in android activity.

I have all ready work on back button in onKeyReleased() in C++ , but when I check back button in activity methods like onBackPressed() , onKeyUp() , OnKeyDown() e.t.c then I get nothing.

Please help me with the same.

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701

2 Answers2

8

Well you have to create a new event listener for the keyboard in the init of your class. Because I don't know what you have at this moment, this is how I implement the back button listener for Android:

bool YourScene::init()
{
    if(!Layer::init()) return false;
    auto listener = EventListenerKeyboard::create();
    listener->onKeyReleased = CC_CALLBACK_2(YourScene::onKeyReleased, this);
    Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);
    ...........
}

void YourScene::onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event)
{
    if(keyCode == EventKeyboard::KeyCode::KEY_BACK)
    {
        // IMPLEMENT YOUR BACK BUTTON LOGIN HERE
    }
}
Iulian
  • 1,156
  • 11
  • 19
0

You can use this method, It is already inside the class Cocos2dxGLSurfaceView.java, you can handle your activity from here

    @Override
    public boolean onKeyDown(final int pKeyCode, final KeyEvent pKeyEvent) {
}
Pratik
  • 413
  • 3
  • 6