diff --git a/cocos2dx/text_input_node/CCTextFieldTTF.cpp b/cocos2dx/text_input_node/CCTextFieldTTF.cpp index 273504f775..67c9340396 100644 --- a/cocos2dx/text_input_node/CCTextFieldTTF.cpp +++ b/cocos2dx/text_input_node/CCTextFieldTTF.cpp @@ -179,27 +179,33 @@ void CCTextFieldTTF::insertText(const char * text, int len) len = nPos; sInsert.erase(nPos); } - if (len <= 0) + + if (len > 0) { - // close keyboard - CCEGLView * pGlView = CCDirector::sharedDirector()->getOpenGLView(); - if (pGlView) + if (m_pDelegate && m_pDelegate->onTextFieldInsertText(this, sInsert.c_str(), len)) { - pGlView->setIMEKeyboardState(false); + // delegate doesn't want insert text + return; } - return; + + m_nCharCount += _calcCharCount(sInsert.c_str()); + std::string sText(*m_pInputText); + sText.append(sInsert); + setString(sText.c_str()); } - if (m_pDelegate && m_pDelegate->onTextFieldInsertText(this, sInsert.c_str(), len)) + if (sInsert.npos == nPos) { + return; + } + + // '\n' has inserted, let delegate process first + if (m_pDelegate && m_pDelegate->onTextFieldInsertText(this, "\n", 1)) { - // delegate doesn't want insert text return; } - - m_nCharCount += _calcCharCount(sInsert.c_str()); - std::string sText(*m_pInputText); - sText.append(sInsert); - setString(sText.c_str()); + + // if lelegate hasn't process, detach with ime as default + detachWithIME(); } void CCTextFieldTTF::deleteBackward() diff --git a/tests/tests/TextInputTest/TextInputTest.cpp b/tests/tests/TextInputTest/TextInputTest.cpp index f3c7010381..b0a9cc734e 100644 --- a/tests/tests/TextInputTest/TextInputTest.cpp +++ b/tests/tests/TextInputTest/TextInputTest.cpp @@ -368,18 +368,18 @@ bool TextFieldTTFActionTest::onTextFieldDetachWithIME(CCTextFieldTTF * pSender) bool TextFieldTTFActionTest::onTextFieldInsertText(CCTextFieldTTF * pSender, const char * text, int nLen) { + // if insert enter, treat as default to detach with ime + if ('\n' == *text) + { + return false; + } + // if the textfield's char count more than m_nCharLimit, doesn't insert text anymore. if (pSender->getCharCount() >= m_nCharLimit) { return true; } - // if insert enter, treat as default - if ('\n' == *text) - { - return false; - } - // create a insert text sprite and do some action CCLabelTTF * label = CCLabelTTF::labelWithString(text, FONT_NAME, FONT_SIZE); this->addChild(label);