游戏虚拟键盘盘怎么放入游戏中

Cocos2d—X游戏开发之CCEditBox(虚拟键盘高级篇)(十) - 推酷
Cocos2d—X游戏开发之CCEditBox(虚拟键盘高级篇)(十)
看到这么好的输入控件,一定要跟大家分享哈。
使用CCTextField的时候,各种纠结哈,血泪史就不多说了。
先介绍下这个令人使用极其舒服的扩展控件,首先感谢他的作者,James Chen(虽然我不知道他是谁,笑)。
Copyright (c)
cocos2d-x.org
Copyright (c) 2012 James Chen
呵呵,现在看下这个CCEditBox的源码吧。
S1,首先看下,它为我们提供的虚拟键盘各种模式吧
这是键盘完成按钮的模式,从默认到done、go等。
enum KeyboardReturnType {
kKeyboardReturnTypeDefault = 0,
kKeyboardReturnTypeDone,
kKeyboardReturnTypeSend,
kKeyboardReturnTypeSearch,
kKeyboardReturnTypeGo
这是键盘的输入模式,有数字,电话,和邮件等。
enum EditBoxInputMode
* The user is allowed to enter any text, including line breaks.
kEditBoxInputModeAny = 0,
* The user is allowed to enter an e-mail address.
kEditBoxInputModeEmailAddr,
* The user is allowed to enter an integer value.
kEditBoxInputModeNumeric,
* The user is allowed to enter a phone number.
kEditBoxInputModePhoneNumber,
* The user is allowed to enter a URL.
kEditBoxInputModeUrl,
* The user is allowed to enter a real number value.
* This extends kEditBoxInputModeNumeric by allowing a decimal point.
kEditBoxInputModeDecimal,
* The user is allowed to enter any text, except for line breaks.
kEditBoxInputModeSingleLine
然后,就是输入框的标记,有些输入密码的时候要的星号就在这里了。
enum EditBoxInputFlag
* Indicates that the text entered is confidential data that should be
* obscured whenever possible. This implies EDIT_BOX_INPUT_FLAG_SENSITIVE.
kEditBoxInputFlagPassword = 0,
* Indicates that the text entered is sensitive data that the
* implementation must never store into a dictionary or table for use
* in predictive, auto-completing, or other accelerated input schemes.
* A credit card number is an example of sensitive data.
kEditBoxInputFlagSensitive,
* This flag is a hint to the implementation that during text editing,
* the initial letter of each word should be capitalized.
kEditBoxInputFlagInitialCapsWord,
* This flag is a hint to the implementation that during text editing,
* the initial letter of each sentence should be capitalized.
kEditBoxInputFlagInitialCapsSentence,
* Capitalize all characters automatically.
kEditBoxInputFlagInitialCapsAllCharacters
S2,看下CCEditBox的源代码:
/****************************************************************************
Copyright (c)
cocos2d-x.org
Copyright (c) 2012 James Chen
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the &Software&), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED &AS IS&, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include &CCEditBox.h&
#include &CCEditBoxImpl.h&
NS_CC_EXT_BEGIN
CCEditBox::CCEditBox(void)
: m_pEditBoxImpl(NULL)
, m_pDelegate(NULL)
, m_eEditBoxInputMode(kEditBoxInputModeSingleLine)
, m_eEditBoxInputFlag(kEditBoxInputFlagInitialCapsAllCharacters)
, m_eKeyboardReturnType(kKeyboardReturnTypeDefault)
, m_nFontSize(-1)
, m_nPlaceholderFontSize(-1)
, m_colText(ccWHITE)
, m_colPlaceHolder(ccGRAY)
, m_nMaxLength(0)
, m_fAdjustHeight(0.0f)
, m_nScriptEditBoxHandler(0)
CCEditBox::~CCEditBox(void)
CC_SAFE_DELETE(m_pEditBoxImpl);
unregisterScriptEditBoxHandler();
void CCEditBox::touchDownAction(CCObject *sender, CCControlEvent controlEvent)
m_pEditBoxImpl-&openKeyboard();
CCEditBox* CCEditBox::create(const CCSize& size, CCScale9Sprite* pNormal9SpriteBg, CCScale9Sprite* pPressed9SpriteBg/* = NULL*/, CCScale9Sprite* pDisabled9SpriteBg/* = NULL*/)
CCEditBox* pRet = new CCEditBox();
if (pRet != NULL && pRet-&initWithSizeAndBackgroundSprite(size, pNormal9SpriteBg))
if (pPressed9SpriteBg != NULL)
pRet-&setBackgroundSpriteForState(pPressed9SpriteBg, CCControlStateHighlighted);
if (pDisabled9SpriteBg != NULL)
pRet-&setBackgroundSpriteForState(pDisabled9SpriteBg, CCControlStateDisabled);
pRet-&autorelease();
CC_SAFE_DELETE(pRet);
bool CCEditBox::initWithSizeAndBackgroundSprite(const CCSize& size, CCScale9Sprite* pPressed9SpriteBg)
if (CCControlButton::initWithBackgroundSprite(pPressed9SpriteBg))
m_pEditBoxImpl = __createSystemEditBox(this);
m_pEditBoxImpl-&initWithSize(size);
this-&setZoomOnTouchDown(false);
this-&setPreferredSize(size);
this-&setPosition(ccp(0, 0));
this-&addTargetWithActionForControlEvent(this, cccontrol_selector(CCEditBox::touchDownAction), CCControlEventTouchUpInside);
void CCEditBox::setDelegate(CCEditBoxDelegate* pDelegate)
m_pDelegate = pD
if (m_pEditBoxImpl != NULL)
m_pEditBoxImpl-&setDelegate(pDelegate);
CCEditBoxDelegate* CCEditBox::getDelegate()
return m_pD
void CCEditBox::setText(const char* pText)
if (pText != NULL)
m_strText = pT
if (m_pEditBoxImpl != NULL)
m_pEditBoxImpl-&setText(pText);
const char* CCEditBox::getText(void)
if (m_pEditBoxImpl != NULL)
const char* pText = m_pEditBoxImpl-&getText();
if(pText != NULL)
return &&;
void CCEditBox::setFont(const char* pFontName, int fontSize)
m_strFontName = pFontN
m_nFontSize = fontS
if (pFontName != NULL)
if (m_pEditBoxImpl != NULL)
m_pEditBoxImpl-&setFont(pFontName, fontSize);
void CCEditBox::setFontName(const char* pFontName)
m_strFontName = pFontN
if (m_pEditBoxImpl != NULL && m_nFontSize != -1)
m_pEditBoxImpl-&setFont(pFontName, m_nFontSize);
void CCEditBox::setFontSize(int fontSize)
m_nFontSize = fontS
if (m_pEditBoxImpl != NULL && m_strFontName.length() & 0)
m_pEditBoxImpl-&setFont(m_strFontName.c_str(), m_nFontSize);
void CCEditBox::setFontColor(const ccColor3B& color)
m_colText =
if (m_pEditBoxImpl != NULL)
m_pEditBoxImpl-&setFontColor(color);
void CCEditBox::setPlaceholderFont(const char* pFontName, int fontSize)
m_strPlaceholderFontName = pFontN
m_nPlaceholderFontSize = fontS
if (pFontName != NULL)
if (m_pEditBoxImpl != NULL)
m_pEditBoxImpl-&setPlaceholderFont(pFontName, fontSize);
void CCEditBox::setPlaceholderFontName(const char* pFontName)
m_strPlaceholderFontName = pFontN
if (m_pEditBoxImpl != NULL && m_nPlaceholderFontSize != -1)
m_pEditBoxImpl-&setPlaceholderFont(pFontName, m_nFontSize);
void CCEditBox::setPlaceholderFontSize(int fontSize)
m_nPlaceholderFontSize = fontS
if (m_pEditBoxImpl != NULL && m_strPlaceholderFontName.length() & 0)
m_pEditBoxImpl-&setPlaceholderFont(m_strPlaceholderFontName.c_str(), m_nFontSize);
void CCEditBox::setPlaceholderFontColor(const ccColor3B& color)
m_colText =
if (m_pEditBoxImpl != NULL)
m_pEditBoxImpl-&setPlaceholderFontColor(color);
void CCEditBox::setPlaceHolder(const char* pText)
if (pText != NULL)
m_strPlaceHolder = pT
if (m_pEditBoxImpl != NULL)
m_pEditBoxImpl-&setPlaceHolder(pText);
const char* CCEditBox::getPlaceHolder(void)
return m_strPlaceHolder.c_str();
void CCEditBox::setInputMode(EditBoxInputMode inputMode)
m_eEditBoxInputMode = inputM
if (m_pEditBoxImpl != NULL)
m_pEditBoxImpl-&setInputMode(inputMode);
void CCEditBox::setMaxLength(int maxLength)
m_nMaxLength = maxL
if (m_pEditBoxImpl != NULL)
m_pEditBoxImpl-&setMaxLength(maxLength);
int CCEditBox::getMaxLength()
return m_nMaxL
void CCEditBox::setInputFlag(EditBoxInputFlag inputFlag)
m_eEditBoxInputFlag = inputF
if (m_pEditBoxImpl != NULL)
m_pEditBoxImpl-&setInputFlag(inputFlag);
void CCEditBox::setReturnType(KeyboardReturnType returnType)
if (m_pEditBoxImpl != NULL)
m_pEditBoxImpl-&setReturnType(returnType);
/* override function */
void CCEditBox::setPosition(const CCPoint& pos)
CCControlButton::setPosition(pos);
if (m_pEditBoxImpl != NULL)
m_pEditBoxImpl-&setPosition(pos);
void CCEditBox::setVisible(bool visible)
CCControlButton::setVisible(visible);
if (m_pEditBoxImpl != NULL)
m_pEditBoxImpl-&setVisible(visible);
void CCEditBox::setContentSize(const CCSize& size)
CCControlButton::setContentSize(size);
if (m_pEditBoxImpl != NULL)
m_pEditBoxImpl-&setContentSize(size);
void CCEditBox::setAnchorPoint(const CCPoint& anchorPoint)
CCControlButton::setAnchorPoint(anchorPoint);
if (m_pEditBoxImpl != NULL)
m_pEditBoxImpl-&setAnchorPoint(anchorPoint);
void CCEditBox::visit(void)
CCControlButton::visit();
if (m_pEditBoxImpl != NULL)
m_pEditBoxImpl-&visit();
void CCEditBox::onEnter(void)
CCControlButton::onEnter();
if (m_pEditBoxImpl != NULL)
m_pEditBoxImpl-&onEnter();
void CCEditBox::onExit(void)
CCControlButton::onExit();
if (m_pEditBoxImpl != NULL)
// remove system edit control
m_pEditBoxImpl-&closeKeyboard();
static CCRect getRect(CCNode * pNode)
CCSize contentSize = pNode-&getContentSize();
CCRect rect = CCRectMake(0, 0, contentSize.width, contentSize.height);
return CCRectApplyAffineTransform(rect, pNode-&nodeToWorldTransform());
void CCEditBox::keyboardWillShow(CCIMEKeyboardNotificationInfo& info)
// CCLOG(&CCEditBox::keyboardWillShow&);
CCRect rectTracked = getRect(this);
// some adjustment for margin between the keyboard and the edit box.
rectTracked.origin.y -= 4;
// if the keyboard area doesn't intersect with the tracking node area, nothing needs to be done.
if (!rectTracked.intersectsRect(info.end))
CCLOG(&needn't to adjust view layout.&);
// assume keyboard at the bottom of screen, calculate the vertical adjustment.
m_fAdjustHeight = info.end.getMaxY() - rectTracked.getMinY();
// CCLOG(&CCEditBox:needAdjustVerticalPosition(%f)&, m_fAdjustHeight);
if (m_pEditBoxImpl != NULL)
m_pEditBoxImpl-&doAnimationWhenKeyboardMove(info.duration, m_fAdjustHeight);
void CCEditBox::keyboardDidShow(CCIMEKeyboardNotificationInfo& info)
void CCEditBox::keyboardWillHide(CCIMEKeyboardNotificationInfo& info)
// CCLOG(&CCEditBox::keyboardWillHide&);
if (m_pEditBoxImpl != NULL)
m_pEditBoxImpl-&doAnimationWhenKeyboardMove(info.duration, -m_fAdjustHeight);
void CCEditBox::keyboardDidHide(CCIMEKeyboardNotificationInfo& info)
void CCEditBox::registerScriptEditBoxHandler(int handler)
unregisterScriptEditBoxHandler();
m_nScriptEditBoxHandler =
void CCEditBox::unregisterScriptEditBoxHandler(void)
if (0 != m_nScriptEditBoxHandler)
CCScriptEngineManager::sharedManager()-&getScriptEngine()-&removeScriptHandler(m_nScriptEditBoxHandler);
m_nScriptEditBoxHandler = 0;
NS_CC_EXT_END
当然,我们主要使用的方法主要是设置位置,大小,字体属性,键盘模式,长度,设置和获取键盘输入的内容。
S3,看下我们怎么使用这个类来实现我们的虚拟键盘,ps:键盘缩回的神马问题在这里都是浮云
首先,继承这个CCEditBoxDelegate。重写这些方法,但是基本上不做什么。
virtual void editBoxEditingDidBegin(CCEditBox* editBox) ;
virtual void editBoxEditingDidEnd(CCEditBox* editBox) ;
virtual void editBoxTextChanged(CCEditBox* editBox, const std::string& text) ;
virtual void editBoxReturn(CCEditBox* editBox) ;
然后,就设置EditBox的属性控制。废话不说,看代码:
//设置CCEditBox的输入框的背景图片,CCScale9Sprite是可拉伸
CCScale9Sprite* sacel9Spr = CCScale9Sprite::create(&Icon@2x.png&);
//设置的长和宽,和背景图片
box = CCEditBox::create(CCSizeMake(300, 100), sacel9Spr);
//设置位置
box-&setPosition(ccp(size.width/2, size.height/2));
//设置字体颜色
box-&setFontColor(ccc3(255, 228, 173));
//设置输入框提示字
box-&setPlaceHolder(&length 5&);
//设置键盘输入模式
box-&setInputMode(kEditBoxInputModeAny);
//设置键盘输入的字符的首写大写
box-&setInputFlag(kEditBoxInputFlagInitialCapsWord);
//设置键盘缩回按钮为done
box-&setReturnType(kKeyboardReturnTypeDone);
//设置代理
box-&setDelegate(this);
//设置输入长度为20个字符
box-&setMaxLength(20);
this-&addChild(box, 1);
下面贴图一张,看下效果:
已发表评论数()
已收藏到推刊!
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
排版有问题
没有分页内容
视频无法显示
图片无法显示新一代游戏神器?NEC开发虚拟键盘设备
新一代游戏神器?NEC开发虚拟键盘设备
导读:几年之前,当虚拟键盘第一次亮相的时候,大家都对这项炫酷的新技术无比向往。近日,NEC宣布,他们正在研发一项技术,那就是真正的虚拟键盘,虽然他们还没能完全掌握这项技术,不过这个想法还是非常可圈可点的。
  【讯】11月16日消息,几年之前,当虚拟键盘第一次亮相的时候,大家都对这项炫酷的新技术无比向往。近日,NEC宣布,他们正在研发一项技术,那就是真正的虚拟键盘,虽然他们还没能完全掌握这项技术,不过这个想法还是非常可圈可点的。
  日前,NEC宣布,他们正在研发一项技术,那就是真正的虚拟键盘,不过与之前展示的那种镭射形式不同,他们的作品与HoloLens的原理相似。以下就是演示视频,一起来看一下:
  这款产品仍然在设计的初期阶段,他们将其命名为ARmKeypad(中文意思就是&手臂键盘&的意思)。正如其名字所暗示,这不是一款在桌面之类的平面上投射到键盘,而是一款可穿戴的虚拟配件产品。而且,这是一款完全解放双手的可穿戴设备,大大提高了生产效率。比如,人们不必再一手拿着键盘,一手工作了。需要的时候,直接在胳膊上点几下就搞定了。
  这款虚拟键盘设备,除了需要佩戴眼镜之外,还需要在手腕上佩戴一款内置了传感器的智能手表。眼睛的功能是可以让使用者在手臂上看到键盘的影像,手表则负责感应手指的位置,来触发相应的键盘按键。其开发者称:感应器支持高速和高精准度的输入,就像在实体键盘上敲击一样。
  这款设备,预计将在明年正式发布。随着可穿戴设备大潮的袭来,会有越来越多类似的设备被开发出来,人类的生活也将进一步改变。}

我要回帖

更多关于 虚拟游戏键盘汉化版 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信