Cocos2d-x add virtual keyboard get user input CCTextField instance

Source: Internet
Author: User

Today, I will share with you how to get the virtual keyboard and user input in the cocos2d-x. Sometimes, you need to enter some information, such as user logon. At this time, you need to obtain the virtual keyboard and the input information. Here we use CCTextField to obtain the user input. CCTextFieldTTF is a subclass of CCLabelTTF, which is a dynamic CCLabelTTF. Let's write an example to see how to use CCTextFieldTTF. 1. First, create a Text project. The helloscene class inherits CCLayer and CCTextFieldDelegate. Some functions need to be declared in the header file. Let's look at the code: [cpp] class HelloWorld: public cocos2d: CCLayer, cocos2d: CCTextFieldDelegate // multi-class inheritance {public: virtual bool init (); // rewrite the callback function of CCTextFieldDelegate when the user starts the virtual keyboard. // The virtual bool onTextFieldAttachWithIME (CCTextFieldTTF * sender) must be exported ); // The callback function virtual bool onTextFieldDetachWithIME (CCTextFieldTTF * sender) when the user disables the virtual keyboard; // The callback function virtual bool onTextFieldInsertText (CCText FieldTTF * sender, const char * text, int nLen); // callback function virtual bool onTextFieldDeleteBackward (CCTextFieldTTF * sender, const char * delText, int nLen); static cocos2d: CCScene * scene (); void menuCloseCallback (CCObject * pSender); CREATE_FUNC (HelloWorld) ;}; 2. Let's go to the source file to implement them: here, the source file's original init function and scene function are not moved, but changed the callback function menuCloseCallback: [cpp] void HelloWorld: menuCloseCallback (CCObject * pSen Der) {CCTextFieldTTF * text = CCTextFieldTTF: textfieldwithzaceholder ("plase input", "Arial", 24); // the text box> setPosition (ccp (100,100 )); addChild (text); text-> setDelegate (this); // bind the interface text-> attachWithIME (); // enable the virtual keyboard // text-> detachWithIME (); remember to comment it out, otherwise it will be turned off again // close the virtual keyboard} [cpp] bool HelloWorld: onTextFieldAttachWithIME (CCTextFieldTTF * sender) {return false; // false indicates enable the keyboard} // callback function bool El when the user disables the virtual keyboard LoWorld: onTextFieldDetachWithIME (CCTextFieldTTF * sender) {return false; // close the keyboard} // callback function bool HelloWorld: onTextFieldInsertText (CCTextFieldTTF * sender, const char * text, int nLen) {return false; // input character} // callback function bool HelloWorld: onTextFieldDeleteBackward (CCTextFieldTTF * sender, const char * delText, int nLen) {return false; // delete character} OK, you can run it. What about my virtual keyboard? Windows does not have a virtual keyboard. You can only view the virtual keyboard after compiling the project to android or ios.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.