天天看點

在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);