I'm reading user input from GLFW using callbacks (set using glfwSetCharCallback) according to this page: http://www.glfw.org/docs/latest/input.html#input_char
The callback function recieves the pressed key as a 32-bit unsigned int. How can I convert this to something that I can print on my screen? I've tried both codecvt from C++11 and the ICU library, but couldn't get anything to print readable characters to my terminal.
This is the code of my callback function:
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;
}
If I do nothing to the input (key), nothing's shown at all. Just a blank line.
You're calling ucnv_toUChars(), which converts an 8-bit string to Unicode. But you need to go the other way, and convert your Unicode string to 8-bit, such as UTF-8. I would use the UnicodeString class, which has a constructor taking UTF-32, and a method toUTF8String().