天天看点

在cocos2d项目中添加android手机按返回键功能

//Android返回键事件

auto returnEvent = EventListenerKeyboard::create();

returnEvent->onKeyReleased = [&](EventKeyboard::KeyCode key, Event* event)

{ //下面这段代码是通过键值来判断是否触摸返回键,由于有些手机返回键的键值不同,暂时就只列举了三种(我也不清楚键值有多少,也是搬的别人获取的键值),可以自行添加

if (EventKeyboard::KeyCode::KEY_RETURN == key || EventKeyboard::KeyCode::KEY_ESCAPE == key || EventKeyboard::KeyCode::KEY_BACKSPACE == key)

{

Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

exit(0);

#endif

}

};

this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(returnEvent, this);