GLFW: input & timeGLFW 2.4

glfwSetCharCallback

Registers a function that receives TEXT typed by the user, one Unicode character at a time.

Signature

GLFWcharfun glfwSetCharCallback(GLFWwindow* window, GLFWcharfun callback);

How it works

Keys are not characters. On a Brazilian keyboard, typing "ĂŁ" is two key presses (~ then a) but one character; Shift+2 is "@" on one layout and "\"" on another. The char callback receives the final character after the system applied the layout, Shift and accents.

The value is a Unicode code point (an unsigned int). Convert it to UTF-8 to add it to a string.

Parameters

windowGLFWwindow*

The window to listen to.

callbackGLFWcharfun

Your function, void f(GLFWwindow* window, unsigned int codepoint), or NULL.

Return value

The callback that was set before, or NULL.

Tips & pitfalls

  • Backspace, Enter and arrows produce no character: handle them in the key callback.

Errors

When the call is invalid it does nothing and sets an error, which glGetError returns:

GLFW_NOT_INITIALIZED
Called before glfwInit or after glfwTerminate.

Example

example.cpp
1std::u32string typed;
2glfwSetCharCallback(window, [](GLFWwindow*, unsigned int cp) {
3    typed.push_back(static_cast<char32_t>(cp));   // 'a', 'ã', '@', '€'...
4});
Official GLFW documentation