标签:textfieldttf textfield 中文乱码 输入框 cocos2dx3.x
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">最近开发cocos2dx 项目,做一个小游戏。(个人喜欢用最新版本)</span>
没系统学习就是问题多多,遇到了很多问题,比如所有的opengl api都必须在主线程中调用,
这让我在多线程加载方面吃了不少亏,浪费了很多时间。
这次遇到了输入框中输入中文出现乱码问题。搜索了很多方法,都没找到。没办法。但是知道cocos2dx 2.x版本没有这个问题,所以就只有跟进代码了。
首先 找到2.x中找到WM_CHAR的派发消息:
在platform/win32/下的CCEGLView.cpp 中 LRESULT CCEGLView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)里面可以找到,
不说了,直接贴代码:
case WM_CHAR: { if (wParam < 0x20) { if (VK_BACK == wParam) { CCIMEDispatcher::sharedDispatcher()->dispatchDeleteBackward(); } else if (VK_RETURN == wParam) { CCIMEDispatcher::sharedDispatcher()->dispatchInsertText("\n", 1); } else if (VK_TAB == wParam) { // tab input } else if (VK_ESCAPE == wParam) { // ESC input //CCDirector::sharedDirector()->end(); } } else if (wParam < 128) { // ascii char CCIMEDispatcher::sharedDispatcher()->dispatchInsertText((const char *)&wParam, 1); } else { char szUtf8[8] = {0}; int nLen = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)&wParam, 1, szUtf8, sizeof(szUtf8), NULL, NULL); CCIMEDispatcher::sharedDispatcher()->dispatchInsertText(szUtf8, nLen); } if ( m_lpfnAccelerometerKeyHook!=NULL ) { (*m_lpfnAccelerometerKeyHook)( message,wParam,lParam ); } } break;
else if (wParam < 128) { // ascii char CCIMEDispatcher::sharedDispatcher()->dispatchInsertText((const char *)&wParam, 1); } else { char szUtf8[8] = {0}; int nLen = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)&wParam, 1, szUtf8, sizeof(szUtf8), NULL, NULL); CCIMEDispatcher::sharedDispatcher()->dispatchInsertText(szUtf8, nLen); }
好了,进入我们3.2版本里面:
首先看主注册消息事件开始处:
在GLView::initWithRect中:
glfwSetMouseButtonCallback(_mainWindow,
GLFWEventHandler::onGLFWMouseCallBack);
glfwSetCursorPosCallback(_mainWindow, GLFWEventHandler::onGLFWMouseMoveCallBack);
glfwSetScrollCallback(_mainWindow, GLFWEventHandler::onGLFWMouseScrollCallback);
glfwSetCharCallback(_mainWindow, GLFWEventHandler::onGLFWCharCallback);
glfwSetKeyCallback(_mainWindow, GLFWEventHandler::onGLFWKeyCallback);
glfwSetWindowPosCallback(_mainWindow, GLFWEventHandler::onGLFWWindowPosCallback);
glfwSetFramebufferSizeCallback(_mainWindow, GLFWEventHandler::onGLFWframebuffersize);
glfwSetWindowSizeCallback(_mainWindow, GLFWEventHandler::onGLFWWindowSizeFunCallback);
上面红色就是我们想要的:
进入GLFWEventHandler::onGLFWCharCallback,在跟进: _view->onGLFWCharCallback(window, character);
void GLView::onGLFWCharCallback(GLFWwindow *window, unsigned int character) { IMEDispatcher::sharedDispatcher()->dispatchInsertText((const char*)&character, 1); }
void GLView::onGLFWCharCallback(GLFWwindow *window, unsigned int character) { if (character < 128) IMEDispatcher::sharedDispatcher()->dispatchInsertText((const char*)&character, 1); else { char szUtf8[8] = { 0 }; int nLen = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)&character, 1, szUtf8, sizeof(szUtf8), NULL, NULL); IMEDispatcher::sharedDispatcher()->dispatchInsertText(szUtf8, nLen); } }
GLView::onGLFWCharCallback
然后添加以上函数内容,即搞定。
学习交流!!!紧供参考
cocos2dx 3.2 解决输入框(TextField,TextFieldTTF) windows下输入法中文乱码问题,布布扣,bubuko.com
cocos2dx 3.2 解决输入框(TextField,TextFieldTTF) windows下输入法中文乱码问题
标签:textfieldttf textfield 中文乱码 输入框 cocos2dx3.x
原文地址:http://blog.csdn.net/xie1xiao1jun/article/details/38711849