COCOS2DX Monitor the Android Device button response is very simple, directly on the code
Register a listener, the same way as the touch Monitor, 3.x after the practice is like this
auto listener = EventListenerKeyboard::create();listener -> onKeyReleased = CC_CALLBACK_2(HelloWorld::onKeyReleased, this);//CC_CALLBACK_2 表示回调函数有两个参数是必须的Director::getInstance() -> getEventDispatcher() -> addEventListenerWithSceneGraphPriority(listener, this);
Then write a function, (if the listener is not much, you can choose to use the Lamda way)
.h
void onKeyReleased(EventKeyboard::KeyCode keycode,Event *pEvent);
.cpp
void HelloWorld::onKeyReleased(EventKeyboard::KeyCode keycode, cocos2d::Event *pEvent){ switch (keycode) { case cocos2d::EventKeyboard::KeyCode::KEY_BACK: log("KEY_BACK"); break; case cocos2d::EventKeyboard::KeyCode::KEY_MENU: log("KEY_MENU"); break; default: break;}
Android is commonly used in these two, Windows under the button more ... But it's the same way.
Welcome to visit my blog:helkyle.tk
"Cocos2d-x" 3.x in response to Android's back and menu