Search code examples
c++inputcharglfw

GLFW convert codepoints to characters in glfwCharCallback


I cant figure out how to parse the codepoint in a char in my glfwCharCallback:


std::string currentText;

void char_callback(GLFWwindow* window, unsigned int codepoint)
{
    // currentText += codepoint ???
}

...

glfwSetCharCallback(window, char_callback);


Solution

  • As a start, you can simply truncate the unsigned int to a unsigned char and appending it to your std::string:

    currentText += (unsigned char) codepoint;
    

    Note that this is criminally wrong for anything beyond basic ASCII. As I remarked in the comments, you probably want to either store UTF-32 codepoints for rendering using freetype, or convert to UTF-8 for printing to console or storage.