我正在根据此页面使用回调(使用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;
}
如果我对输入(键)什么都不做,则根本没有显示任何内容。只是一个空白。
答案 0 :(得分:0)
您正在调用ucnv_toUChars(),它将8位字符串转换为Unicode。但是您需要采用其他方式,并将Unicode字符串转换为8位,例如UTF-8。我会使用UnicodeString类,它有一个采用UTF-32的构造函数,以及一个方法toUTF8String()。