COCOS2DX 3.2 Fix input box (TEXTFIELD,TEXTFIELDTTF) The input method in Windows is garbled in Chinese

Source: Internet
Author: User

<span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " > recently developed the COCOS2DX project and made a small game. (Personal likes to use the latest version) </span>

No system learning is a lot of problems, encountered many problems, such as all the OpenGL API must be called in the main thread,

This let me in the multi-threaded loading aspect to eat a lot of losses, wasted a lot of time.

This time encountered in the input box input Chinese garbled problem. Search a lot of methods, have not found. No way. But know that the COCOS2DX 2.x version does not have this problem, so there is only follow-up code.


First find the dispatch message for WM_CHAR found in 2.x:

In CCEGLView.cpp in platform/win32/LRESULT cceglview::windowproc (UINT message, WPARAM WPARAM, LPARAM LPARAM) can be found,

Do not say, direct sticker code:

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 < +) {//ASCII char Ccimedis             Patcher::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;

As above: The most important comes up,

else if (WParam < +)             {                 //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);             

Here we can vaguely see that the Chinese garbled processing method:

Okay, get into our 3.2 version:

First look at the beginning of the main registration message event:

In the Glview::initwithrect:

    Glfwsetmousebuttoncallback (_mainwindow, glfweventhandler::onglfwmousecallback);
    Glfwsetcursorposcallback (_mainwindow, glfweventhandler::onglfwmousemovecallback);
    Glfwsetscrollcallback (_mainwindow, glfweventhandler::onglfwmousescrollcallback);
    glfwsetcharcallback (_mainwindow, Glfweventhandler::o Nglfwcharcallback);
    Glfwsetkeycallback (_mainwindow, Glfweventhandler::onglfwkeycallback );
    Glfwsetwindowposcallback (_mainwindow, Glfweventhandler::o Nglfwwindowposcallback);
    Glfwsetframebuffersizecallback (_mainwindow, Glfweventhandler::o nglfwframebuffersize);
    Glfwsetwindowsizecallback (_mainwindow, Glfweventhandler::o Nglfwwindowsizefuncallback);


The red above is what we want:

Enter Glfweventhandler::onglfwcharcallback, in follow up: _view->onglfwcharcallback (window, character);


OK: We go to the most critical location: Direct Sticker Code:
void Glview::onglfwcharcallback (Glfwwindow *window, unsigned int character) {Imedispatcher::shareddispatcher () Dispatchinserttext ((const char*) &character, 1);}

Does it have a similar place with 2.x?Yes, we use 2.x to deal with the problem of Chinese garbled, not much to say, directly see the results
void Glview::onglfwcharcallback (Glfwwindow *window, unsigned int character) {if (character < +) 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);}}

OK, finish! It's that simple, now you can try it, support Chinese input method (mainly for Windows platform)

in fact, one step: in 3.x to collect the cable
Glview::onglfwcharcallback
Then add the above function content, that is done.
Learning Communication!!! Compact for reference
Related Article

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.