Cocos2d-x input box (CCTextFieldTTF)

Source: Internet
Author: User

1. The current class inherits public cocos2d: CCTextFieldDelegate to implement delegated listening.

2. Declare in. h:

// Rewrite the callback function of CCTextFieldDelegate.
// Callback function when the user starts the virtual keyboard
// You must have an exit
Virtual bool onTextFieldAttachWithIME (CCTextFieldTTF * sender );
// Callback function when the user disables the virtual keyboard
Virtual bool onTextFieldDetachWithIME (CCTextFieldTTF * sender );
// Callback function when the user inputs the virtual keyboard
Virtual bool onTextFieldInsertText (CCTextFieldTTF * sender, const char * text, int nLen );
// Callback function when the user deletes the text virtual keyboard
Virtual bool onTextFieldDeleteBackward (CCTextFieldTTF * sender, const char * delText, int nLen );

3. Implement in the cpp File

1 ),

CCSize size = CCDirector: shareddire()-> getWinSize ();
CCTextFieldTTF * textField = CCTextFieldTTF: textfieldwit2d-aceholder ("click out input...", "Helvetica", 24 );
TextField-> setPosition (ccp (size. width * 0.5, size. height * 0.7 ));
AddChild (textField );
// Bind the interface
TextField-> setDelegate (this );
// Enable Input
TextField-> attachWithIME ();
// Close the input
// TextField-> detachWithIME ();

2) header file listening method implementation

// Callback function when the user starts the virtual keyboard
Bool HelloWorld: onTextFieldAttachWithIME (CCTextFieldTTF * pSender)
{
CCLOG ("START input ");
// Return false;
// Return true: Do not start
}
// Callback function when the user disables the virtual keyboard
Bool HelloWorld: onTextFieldDetachWithIME (CCTextFieldTTF * pSender)
{
CCLOG ("Close input ");
Return false;
// Return true: Do not close
}
// Callback function when the user inputs
Bool HelloWorld: onTextFieldInsertText (CCTextFieldTTF * pSender, const char * text, int nLen)
{
CCLOG ("input character ...");
Return false;
// Return true: no characters are entered.

}
// Callback function when the user deletes text
Bool HelloWorld: onTextFieldDeleteBackward (CCTextFieldTTF * pSender, const char * delText, int nLen)
{
CCLOG ("delete character ");
Return false;
// Return true: Do not delete
}

4. Note: in win32, the virtual keyboard does not pop up and you can simply enter it in the keyboard.

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.