cocos2d-x-4.0 Windows 添加按键捕获

1:

  去“bool AppDelegate::applicationDidFinishLaunching()”函数中,找到那个创建显示区的类。

  即找到调用“scene()”这个函数的类。

2:

  找到了之后,去创建显示区类里面,找到类初始化函数。

  即找到此类的“init()”函数的实现。

3:

  在“init”函数前面添加一个函数,类似如下

1 void KeyPushUp(EventKeyboard::KeyCode code, Event* event)
2 {
3     char szKey[1024] = "";
4     sprintf_s(szKey, "KeyCode [0x%02X] , Type [%d] \n", code, event->getType());
5     OutputDebugStringA(szKey);
6 };

4:

  在“Init”函数里面,添加以下代码

1     auto listenerKey = EventListenerKeyboard::create();
2     listenerKey->onKeyPressed = KeyPushUp;
3     listenerKey->onKeyReleased = KeyPushUp;
4     auto dispatcher = Director::getInstance()->getEventDispatcher();
5     dispatcher->addEventListenerWithSceneGraphPriority(listenerKey, this);

5:

  然后主窗口就可以接受按键输入的,当然按键输入需要自己在前面处理。

6:

  注意,按键输入只能接收英文输入法下的常规按键输入,如果开了中文输入法的话,无效。

猜你喜欢

转载自www.cnblogs.com/suanguade/p/12390078.html
今日推荐