多平台响应键盘事件!(适用于Cocos2dx 3.0 alpha以上版本号)
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:
首先在须要键盘处理事件的场景中文件里加入例如以下两个函数。
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!
多平台响应键盘事件!(适用于Cocos2dx 3.0 alpha以上版本号)的更多相关文章
- C# WinForm自定义控件响应键盘事件
自己定义的winform控件,用其他键盘事件都无法响应,只有用ProcessCmdKey事件可以达到目的(别忘了主窗体的KeyPreview属性要设置为true),写法如下: prot ...
- WinForm下的键盘事件(KeyPress、KeyDown)及如何处理不响应键盘事件
KeyDown事件用来处理功能键:F1 F2 F3... keyPress事件用来处理字符比如说:A B C... 1 2 3... 注:处理该事件时,需要先将窗体的 KeyPreview=true; ...
- 部分无线终端不响应键盘事件(keydown,keypress,keyup)的解决办法
在无线侧实现搜索显示smartbox功能的时候,会对输入框绑定keydown.keyup.keypress事件,从而在检测到输入框的值发生改变时,发出请求拉取smartbox的内容. 但是,在iPho ...
- 无线端不响应键盘事件(keydown,keypress,keyup)
今天在项目时,在android手机上使用输入法的智能推荐的词的话,不会触发keyup事件,一开始想到在focus时使用一个定时器,每隔100ms检测输入框的值是否发生了改变,如果改变了就作对应的处理, ...
- Win Form不能响应键盘事件
在窗体属性中,将KeyPreview设置为true
- 学习python-跨平台获取键盘事件
class _Getch: """Gets a single character from standard input. Does not echo to the sc ...
- cocos2d-x读取xml(适用于cocos2d-x 2.0以上版本号)
为了能在cocos2d-x的文本标签中显示中文,一个是转换文件编码格式,还有一种就是读取utf-8格式的xml文件.我选择了后者,其原因大家可以去搜索一下cocos2d-x显示中文,希望可以你给答案. ...
- 给Jquery添加alert,prompt方法,类似系统的Alert,Prompt,可以响应键盘,支持拖动
我们在调用系统的Alert,prompt的弹出提示时,不同的系统会有不同的提示框,视觉效果不统一,而且不好看,功能单一,现在我们通过Jquery模拟Alert,prompt,现实统一视觉效果,而且内容 ...
- vuejs监听苹果iphone手机键盘事件
在iphone手机中,vue提供的keyup事件是不能监听iphone键盘的,但是h5提供的input事件可以做到. 只需要向下面这样处理,就可以解决iphone不响应键盘事件的bug <tem ...
随机推荐
- javascript技巧合集
转http://www.blogjava.net/zhaochengming/archive/2010/04/09/317837.html http://www.cnblogs.com/fxgachi ...
- 对加密方式(公钥私钥)的形象理解(以http和https为例)
https其实就是建构在SSL/TLS之上的 http协议,所以要比较https比http多用多少服务器资源,主要看SSL/TLS本身消耗多少服务器资源. http使用TCP 三次握手建立连接,客户端 ...
- Android进阶篇-线程分析(一)
转载自:http://www.trinea.cn/android/java-android%E7%BA%BF%E7%A8%8B%E6%B1%A0/ 介绍new Thread的弊端及Java四种线程池的 ...
- JAVA实现word doc docx pdf excel的在线浏览 - 仿百度文库 源码
我们具体实现思路是这样的 首先下载并安装openoffice和swftools openoffice下载地址:http://www.openoffice.org/download/index.html ...
- 内核驱动中常见的miscdevice、platform_device、platform_driver
最近在看驱动模型,是越看越糊涂,以前接触比较多的都是一些字符驱动,对字符驱动的框架有一定的了解.后来因为想在驱动中实现设备文件的创建,又了解了一下,sysfs文件系统和udev设备文件系统(这两个是两 ...
- FileSystemWatcher使用方法
FileSystemWatcher控件主要功能: 监控指定文件或目录的文件的创建.删除.改动.重命名等活动.可以动态地定义需要监控的文件类型及文件属性改动的类型. 1.常用的几个基本属性: (1) P ...
- 【HDOJ】1356 The Balance
扩展欧几里得的应用. /* 1356 */ #include <iostream> #include <sstream> #include <string> #in ...
- JQuery对XML文件的操作
xml文件如下: <?xml version="1.0" encoding="utf-8" ?> <Root> <Parent&g ...
- Servlet能读到JSessionID,读不到其它cookie问题
Servlet的Cookie值保存与获取 今天测试设置和获取Cookie遇到了一点小问题,很奇怪的问题: 把J2ee服务部署在本地 8080端口:访问任何一个服务时,如果客户端没有cookie,则下发 ...
- 步步为营 SharePoint 开发学习笔记系列总结
转:http://www.cnblogs.com/springyangwc/archive/2011/08/03/2126763.html 概要 为时20多天的sharepoint开发学习笔记系列终于 ...