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!

參考:http://www.cocos2d-x.org/forums/6/topics/39145

多平台响应键盘事件!(适用于Cocos2dx 3.0 alpha以上版本号)的更多相关文章

  1. C# WinForm自定义控件响应键盘事件

    自己定义的winform控件,用其他键盘事件都无法响应,只有用ProcessCmdKey事件可以达到目的(别忘了主窗体的KeyPreview属性要设置为true),写法如下:         prot ...

  2. WinForm下的键盘事件(KeyPress、KeyDown)及如何处理不响应键盘事件

    KeyDown事件用来处理功能键:F1 F2 F3... keyPress事件用来处理字符比如说:A B C... 1 2 3... 注:处理该事件时,需要先将窗体的 KeyPreview=true; ...

  3. 部分无线终端不响应键盘事件(keydown,keypress,keyup)的解决办法

    在无线侧实现搜索显示smartbox功能的时候,会对输入框绑定keydown.keyup.keypress事件,从而在检测到输入框的值发生改变时,发出请求拉取smartbox的内容. 但是,在iPho ...

  4. 无线端不响应键盘事件(keydown,keypress,keyup)

    今天在项目时,在android手机上使用输入法的智能推荐的词的话,不会触发keyup事件,一开始想到在focus时使用一个定时器,每隔100ms检测输入框的值是否发生了改变,如果改变了就作对应的处理, ...

  5. Win Form不能响应键盘事件

    在窗体属性中,将KeyPreview设置为true

  6. 学习python-跨平台获取键盘事件

    class _Getch: """Gets a single character from standard input. Does not echo to the sc ...

  7. cocos2d-x读取xml(适用于cocos2d-x 2.0以上版本号)

    为了能在cocos2d-x的文本标签中显示中文,一个是转换文件编码格式,还有一种就是读取utf-8格式的xml文件.我选择了后者,其原因大家可以去搜索一下cocos2d-x显示中文,希望可以你给答案. ...

  8. 给Jquery添加alert,prompt方法,类似系统的Alert,Prompt,可以响应键盘,支持拖动

    我们在调用系统的Alert,prompt的弹出提示时,不同的系统会有不同的提示框,视觉效果不统一,而且不好看,功能单一,现在我们通过Jquery模拟Alert,prompt,现实统一视觉效果,而且内容 ...

  9. vuejs监听苹果iphone手机键盘事件

    在iphone手机中,vue提供的keyup事件是不能监听iphone键盘的,但是h5提供的input事件可以做到. 只需要向下面这样处理,就可以解决iphone不响应键盘事件的bug <tem ...

随机推荐

  1. 修改Tomcat内存大小

    Windows下,在文件/bin/catalina.bat,Linux下,在文件/bin/catalina.sh的前面,增加如下设置: JAVA_OPTS=-Xms[初始化内存大小] -Xmx[可以使 ...

  2. javaweb学习总结(三十三)——使用JDBC对数据库进行CRUD

    一.statement对象介绍 Jdbc中的statement对象用于向数据库发送SQL语句,想完成对数据库的增删改查,只需要通过这个对象向数据库发送增删改查语句即可. Statement对象的exe ...

  3. 执行gem install dryrun错误

    ERROR:  While executing gem ... (Gem::FilePermissionError)     You don't have write permissions for ...

  4. 【DataStructure In Python】Python模拟链表

    最近一直在学习Python和Perl这两门语言,两者共同点很多,也有不多.希望通过这样的模拟练习可以让自己更熟悉语言,虽然很多时候觉得这样用Python或者Perl并没有体现这两者的真正价值. #! ...

  5. Virtual member call in a constructor

    http://stackoverflow.com/questions/119506/virtual-member-call-in-a-constructor (Assuming you're writ ...

  6. C#中使用自定义的纸张大小

    using System.Drawing.Printing; using System.Drawing; private void Test() { PrintDocument m_pdoc = ne ...

  7. Cookie的Domain

    每个Cookie都有常用的几个元素:name.value.expires.domain Cookie的Domain 设置cookies时,可以设置cookie的域名参数domain,标识cookie在 ...

  8. 用opencv画矩形打上马赛克Mosaic

    /*----------------------------------------------------------------------------- *   *   版权声明: *   可以 ...

  9. Unity3D Mathf函数

    Mathf.Abs绝对值 计算并返回指定参数 f 绝对值. Mathf.Acos反余弦 static function Acos (f : float) : float 以弧度为单位计算并返回参数 f ...

  10. HW4.32

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...