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.