/** * 監聽手機返回鍵 * */ void MyGame::listenKey() { //對手機返回鍵的監聽 auto listenerKeyboard = EventListenerKeyboard::create(); //和回調函數綁定 listenerKeyboard->onKeyReleased = [&](EventKeyboard::KeyCode keyCode, Event * pEvent) { if (keyCode == EventKeyboard::KeyCode::KEY_BACK)// && isTwice == 0) { Director::getInstance()->pause();//遊戲暫停 /* your code*/ } }; //添加到事件分發器中 _eventDispatcher->addEventListenerWithSceneGraphPriority(listenerKeyboard, this); return; }
直接調用這個listenkey 方法就能夠了函數