Search code examples
javainputkeyboardlwjglglfw

GLFW (LWJGL) Input - Holding CTRL down blocks the CharCallback


I try to implement a simple CTRL + V for some textfields inside my game. So I set a flag for CTRL in the KeyCallback whenever ctrl is pressed or released (i tested this one). When I try to type "V" now the CharCallback isn't called. Is it supposed to not work this way? If it is how do i do it right?

That's how I poll it inside the KeyCallback:

if(key == GLFW_KEY_LEFT_CONTROL && action == GLFW_PRESS) {
    controlPressed = true;
}
if(key == GLFW_KEY_LEFT_CONTROL && action == GLFW_RELEASE) {
    controlPressed = false;
}

I use glfwSetKeyCallback(...) to set the callback.


Solution

  • This is intended behavior. The documentation for glfwSetCharCallback says (emphasis mine):

    The character callback behaves as system text input normally does and will not be called if modifier keys are held down that would prevent normal text input on that platform, for example a Super (Command) key on OS X or Alt key on Windows. There is a character with modifiers callback that receives these events.

    However, it also states that you can use a char mods callback, which will be called even when a modifier key is pressed.

    This function sets the character with modifiers callback of the specified window, which is called when a Unicode character is input regardless of what modifier keys are used.

    For example:

    GLFWCharModsCallback charModsCallback = new GLFWCharModsCallback() {
        @Override
        public void invoke(long window, int codepoint, int mods) {
            // do something with the character and modifier flags
        }
    };
    glfwSetCharModsCallback(window, charModsCallback);