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

萝らか妹 提交于 2019-12-10 14:17:29

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/24049840/back-and-home-buttons-pressed-event-for-android-devices-cocos2d-x-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!