1 //如何监听按键事件 2 //创建物理按键的监听事件 3 auto listener = EventListenerKeyboard::create(); 4 //按键被释放时执行的操作,EventKeyboard::KeyCode表示按键代码 5 listener->onKeyReleased = [](EventKeyboard::KeyCode code,Event *e){ 6 log("Key Code : %d",code);//显示按键的值 7 //按下后退键关闭程序 8 switch (code) { 9 //获得物理按键 10 case EventKeyboard::KeyCode::KEY_BACKSPACE: 11 //关闭程序 12 Director::getInstance()->end(); 13 break; 14 15 default: 16 break; 17 } 18 19 20 }; 21 //进行监听 22 Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);
时间: 2024-10-11 07:30:09