多平台响应键盘事件!(适用于Cocos2dx 3.0 alpha以上版本)
2023-09-14 08:57:15 时间
Hello everyone! For a week I’ve been looking on how to make a keyboard work!
I managed to figure it out and I want to share my knowledge with you!
So, before we get started - this tutorial only works on Cocos2d-x 3.0alpha and later.
We will start by making two functions in the scene we want keyboard on.
They will be: 首先在需要键盘处理事件的场景中文件中添加如下两个函数。
Next we need a listener to look for the keyboard we will create it like so (I did it in the init function) 当键盘按下时会调用上面这两的函数。 接下来在init()方法中添加如下代码来监听键盘事件。
Now Our program can detect keyboard! Wait… How do I know what key is pressed? It is simple! Let me show you: 然后就可以在最上面的两个方法中添加keyCode的判定了。
EventKeyboard::KeyCode::KEY_**whatever key** - you will usually get a list of available keys to chose from. Well, I think that’s it! Enjoy! 参考:http://www.cocos2d-x.org/forums/6/topics/39145
从0开发游戏引擎之Win32平台用户输入事件Input类实现 Input类主要实现的功能是 监听用户鼠标键盘事件,当收到操作系统回调之后首先会调到场景管理类,然后通过场景管理类,会调到Input类,然后Input类会传递给KeyInput类和MouseInput处理鼠标和键盘相关的逻辑代码。
I managed to figure it out and I want to share my knowledge with you!
So, before we get started - this tutorial only works on Cocos2d-x 3.0alpha and later.
We will start by making two functions in the scene we want keyboard on.
They will be: 首先在需要键盘处理事件的场景中文件中添加如下两个函数。
OurScene.h: void keyPressed(cocos2d::EventKeyboard::KeyCode keyCode, cocos2d::Event *event); void keyReleased(cocos2d::EventKeyboard::KeyCode keyCode, cocos2d::Event *event); OurScene.cpp: void OurScene::keyPressed(cocos2d::EventKeyboard::KeyCode keyCode, cocos2d::Event *event) void OurScene::keyReleased(cocos2d::EventKeyboard::KeyCode keyCode, cocos2d::Event *event)These functions will be called when we press/release a key on the keyboard.
Next we need a listener to look for the keyboard we will create it like so (I did it in the init function) 当键盘按下时会调用上面这两的函数。 接下来在init()方法中添加如下代码来监听键盘事件。
auto keyboardListener = EventListenerKeyboard::create(); keyboardListener- onKeyPressed = CC_CALLBACK_2(OurScene::keyPressed, this); keyboardListener- onKeyReleased = CC_CALLBACK_2(OurScene::keyReleased, this); EventDispatcher::getInstance()- addEventListenerWithSceneGraphPriority(listener, this); // use if your version is below cocos2d-x 3.0alpha.1 // use this: Director::getInstance()- getEventDispatcher()- addEventListenerWithSceneGraphPriority(listener, this); if you are using cocos2d-x 3.0alpha.1 and later!This code creates a keyboard listener and then setting what functions will be called when the key is pressed or released.
Now Our program can detect keyboard! Wait… How do I know what key is pressed? It is simple! Let me show you: 然后就可以在最上面的两个方法中添加keyCode的判定了。
//put this inside keyPressed or keyReleased if (keyCode == EventKeyboard::KeyCode::KEY_W) CCLog("W key was pressed");This piece of code will check what is the key-code of the key that was pressed. The list of key-codes is inside the EventKeyboard class. To use a keycode you just type:
EventKeyboard::KeyCode::KEY_**whatever key** - you will usually get a list of available keys to chose from. Well, I think that’s it! Enjoy! 参考:http://www.cocos2d-x.org/forums/6/topics/39145
从0开发游戏引擎之Win32平台用户输入事件Input类实现 Input类主要实现的功能是 监听用户鼠标键盘事件,当收到操作系统回调之后首先会调到场景管理类,然后通过场景管理类,会调到Input类,然后Input类会传递给KeyInput类和MouseInput处理鼠标和键盘相关的逻辑代码。
相关文章
- iOS 中的事件响应
- Unity SKFramework框架(十一)、ActionChain 事件链
- js事件循环与macroµ任务队列-前端面试进阶
- PICT:一款功能强大的信息收集和事件响应工具
- 9.14VR行业大事件:高通或在测试下一代VR芯片;VR地下城游戏《Demeo》将于明年登陆PSVR 2
- react+antd组件 modal里面包裹使用form表单并获取提交事件
- 验证和分析Android的事件分发机制
- 2022互联网大事件复盘
- 大事件!密码神器LastPass承认黑客窃取了客户数据
- ThreatHound:一款功能强大的事件响应与威胁搜索辅助工具
- Mysql存储过程、触发器、事件调度器使用入门指南
- Oracle 等待事件 gc current request 官方解释,作用,如何使用及优化方法
- Oracle 等待事件 Enqueues:KO,MultipleObjectCheckpoint 官方解释,作用,如何使用及优化方法
- Go语言使用通道响应计时器的事件
- Go语言使用事件系统实现事件的响应和处理
- SolarWinds 后门事件继续蔓延,微软、Cisco 也成为最新受害者
- javascript一个函数对同一元素的多个事件响应
- jQuery源码分析之Event事件分析
- Javascript事件热键兼容ie|firefox
- JQuery的自定义事件代码,触发,绑定简单实例
- js点击事件链接的问题解决
- MFC中动态创建控件以及事件响应实现方法
- 纯JavaScript实现获取onclick、onchange等事件的值
- JS实现OCX控件的事件响应示例