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. 四大主流云平台对比--CloudStack, Eucalyptus, vCloud Director和OpenStack。

    我迟早可能都要进入的领域,提前温习... 还有KVM,ESXI,API,XEN之间的术语和关系,也要心中有数.. ~~~~~~~~~~~~~~~~~~~ 云计算在如今的IT界一直是一个最热门的话题,鉴 ...

  2. 李洪强漫谈iOS开发[C语言-019]-断点调试

  3. [jobdu]丑数

    由于思维的惯性,用了queue.后来发现一要注意要用集合判重,二是每次往queue里放的多,后来溢出了,要用long long.但这样要用数组,集合,队列,内存多.效率是O(n*logn)的. #in ...

  4. Altium Designer 特定网络取消 remove loops

    在使用Altium Designer时,在PCB Editor 里面可以设定是否需要Automatically remove Loops,但是这个设置是全局的,在设计时难免会遇到对大部分网络是需要删除 ...

  5. 常用的SQL分页算法及对比

    SQL Server 2005引入的新方法. SELECT * FROM (SELECT ROW_NUMBER() OVER(ORDER BY keyField DESC) AS rowNum, * ...

  6. Getting Started Tutorial

    Getting Started Tutorial The topics contained in this section are intended to give you quick exposur ...

  7. 【转】JNI学习积累之一 ---- 常用函数大全

    原文网址:http://blog.csdn.net/qinjuning/article/details/7595104 本文原创,转载请注明出处:http://blog.csdn.net/qinjun ...

  8. Android 实用代码七段(二)

    声明 欢迎转载,但请保留文章原始出处:)  博客园:http://www.cnblogs.com 农民伯伯: http://over140.cnblogs.com 正文 一.获取应用程序下所有Acti ...

  9. (转载)file_get_contents("php://input")

    (转载)http://taoshi.blog.51cto.com/1724747/1165499 $data = file_get_contents("php://input"); ...

  10. Shell获取文件后缀名

    file = "thisfile.txt" echo "filename: ${file%.*}" echo "extension: ${file## ...