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.