键盘的相应事件有两种,按下事件和抬起事件。cocos的Demo在E:\cocos2d-x-3.6\tests\cpp-tests\Classes\NewEventDispatcherTest这个目录下,LabelKeyboardEventTest是其测试代码。代码很简单,随意看下就知道怎么使用了。
下面也实现一个Demo:
auto listener = EventListenerKeyboard ::create (); listener ->onKeyPressed = []( EventKeyboard:: KeyCode keyCode,Event * event ){ log ("KeyPress:%d",keyCode ); }; listener -> onKeyReleased = []( EventKeyboard ::KeyCode keyCode,Event * event ){ log ("KeyRelease:%d",keyCode ); }; _eventDispatcher ->addEventListenerWithSceneGraPHPriority ( listener,this );原文链接:https://www.f2er.com/cocos2dx/340606.html