Back and Home buttons pressed event for Android devices (cocos2d-x 3)
Asked Answered
E

2

5

I have done this to catch Home and Back buttons press events on android devices:

Overrided void Layer::onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event) function like this:

void MyLayer::onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event)
{
    if (keyCode == EventKeyboard::KeyCode::KEY_BACKSPACE /*KeyboardEvent::KeyCode::Menu(KEY_BACKSPACE)*/)
    {
         CCLOG("You pressed back button");
         Director::getInstance()->end();
         exit(0);
    } 
    else if (keyCode == EventKeyboard::KeyCode::KEY_HOME)
    {
         CCLOG("You pressed home button");
         // pause the game
    }
}

also have called setKeypadEnabled(true); in init function of MyLayer. Backspace button closes the game on windows, but no reaction on Home button. Also on Android nothing happens when I press Home or Back. How to get this working on cocos2d-x 3.1?

Evacuation answered 5/6, 2014 at 0:37 Comment(1)
You can't override home button action in android.Megohm
E
8

For catching Back button you need to use EventKeyboard::KeyCode::KEY_ESCAPE. For pausing the game when Home is pressed use void AppDelegate::applicationDidEnterBackground(). There is no way to override home button pushed event.

Evacuation answered 6/6, 2014 at 6:11 Comment(0)
P
2

You can use either EventKeyboard::KeyCode::KEY_BACK or EventKeyboard::KeyCode::KEY_ESCAPE for catching the Android back button event.

Pisciculture answered 23/12, 2014 at 15:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.