Cocos2d-x3.0 捕捉Android的菜单键和返回键
.cpp文件
auto listener = EventListenerKeyboard::create();
listener->onKeyReleased = CC_CALLBACK_2(HelloWorld::onKeyReleased, this);
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);
void HelloWorld::onKeyReleased(EventKeyboard::KeyCode keycode, cocos2d::Event *event){
switch (keycode){
case cocos2d::EventKeyboard::KeyCode::KEY_BACKSPACE:
case cocos2d::EventKeyboard::KeyCode::KEY_BACK:
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.", "Alert");
return;
#endif
Director::getInstance()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
break;
default:
break;
}
}
.h文件
virtual void onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event);
在此前是不够的,一定注意在.h文件中添加USING_NS_CC;否则会报错。