0

根据此页面,我正在使用回调(使用 glfwSetCharCallback 设置)从 GLFW 读取用户输入: http ://www.glfw.org/docs/latest/input.html#input_char

回调函数将按下的键作为 32 位无符号整数接收。如何将其转换为可以在屏幕上打印的内容?我已经尝试过来自 C++11 和 ICU 库的 codecvt,但无法将可读字符打印到我的终端。

这是我的回调函数的代码:

void InputManager::charInputCallback(GLFWwindow* window, unsigned int key)
    {
        UErrorCode errorStatus = U_ZERO_ERROR; //initialize to no error
        UConverter *utf32toUnicode = ucnv_open("UTF-32", &errorStatus); //create converter utf-32 <=> Unicode
        if(U_FAILURE(errorStatus))
            std::cout << u_errorName(errorStatus) << std::endl;
        UChar unicode[10] = {0};
        ucnv_toUChars(utf32toUnicode, unicode, 10, (char*)key, 4, &errorStatus); // this fails with an U_ILLEGAL_ARGUMENT_ERROR
        if(U_FAILURE(errorStatus))
            std::cout << u_errorName(errorStatus) << std::endl;

        std::cout << unicode << std::endl;
    }

如果我对输入(键)不做任何事情,则什么都不会显示。只是一个空行。

4

1 回答 1

0

您正在调用 ucnv_toUChars(),它将 8 位字符串转换为 Unicode。但是您需要另辟蹊径,将您的 Unicode 字符串转换为 8 位,例如 UTF-8。我会使用 UnicodeString 类,它有一个采用 UTF-32 的构造函数和一个 toUTF8String() 方法。

于 2016-09-07T21:38:24.707 回答