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. distinct 去重复查询——两个表join 连接,去掉重复的数据

    ------distinct 去重复查询 select * from  accounts acc join (select distinct accid from roles) r on r.acci ...

  2. HDU4535+公式

    错排公式. 用64位! /* */ #include<stdio.h> #include<string.h> #include<stdlib.h> #include ...

  3. show processlist 执行状态分析

    Sleep状态 通常代表资源未释放,如果是通过连接池,sleep状态应该恒定在一定数量范围内 实战范例:因前端数据输出时(特别是输出到用户终端)未及时关闭数据库连接,导致因网络连接速度产生大量slee ...

  4. easyui源码翻译1.32--ComboTree(树形下拉框)

    前言 扩展自$.fn.combo.defaults和$.fn.tree.defaults.使用$.fn.combotree.defaults重写默认值对象.下载该插件翻译源码 树形下拉框结合选择控件和 ...

  5. js设置radio选中

    在页面数据绑定时,经常会遇到给radio设置选中,以下是我写的js方法,经测试可以使用.欢迎拍砖 <html> <head> <script type="tex ...

  6. Type-C设计上的防护

    Type C设计上各家芯片公司都提供了很多方案,但在防护方面很多留给了客户自己选择,这方面我可以重点聊聊,说起防护,无非就是过压过流防护. 过压防护,Type C的信号线有很多,都需要做静电防护,US ...

  7. 命令行静态编译QT程序

    在.pro文件里加上(非必须)CONFIG += static release windows 静态库必须放在这个路径:C:\Qt\Qt5.3.2_static\bin\qmake -makefile ...

  8. hadoop2.2编程: 数据压缩

    本文主要讨论hadoop的数据压缩与解压缩代码的书写 Compressing and decompressing streams with CompressionCodec import org.ap ...

  9. WPF——传实体类及绑定实体类属性

    public class User: private string _User; public string User1 { get { return _User; } set { _User = v ...

  10. addChildViewController 与 addSubview

    在viewcontrollerA中, 如果想把controllerB.view添加进来, 可以用 addSubview, 但如果controllerB中有个事件, 使用到 self.navigatio ...