Key Callback Not Registering Every Key Press

I am currently trying to get input working on my OpenGL project with GLFW and I seem to have run in to a problem or have missed something when reading the documentation. I am trying to get every key press that happens during a frame and fill a list with the information about that event. I then can query it from anywhere else in my program. If I read the documentation right, the key callback should have every key that was pressed before glfwPollEvents is called next. The only problem is that the callback only ever seems to detect one key event each frame so if I press more than one key it dumps the other keys.

Hi, welcome to the GLFW forums!

It’s possible that you may be hitting a hardware limitation on keyboards for how many particular keys can be registered simultaneously, this is called rollover: https://en.wikipedia.org/wiki/Rollover_(key)

If you want to check that your program is using GLFW correctly you could first try the GLFW test program events.c. If this works but your program doesn’t there may be an error in how you’re using GLFW.

If you’re still having issues then let me know, along with what operating system your using (and which window manager if you’re using Linux).

So I tried the demo and it works just like my implementation does. When I press multiple keys it only tells me that the most recent one I pressed is being held but not the one prior. I’m not exactly sure if my keyboard is the problem as I have a Corsair K70 so this shouldn’t be a problem. I guess my question is can GLFW detect multiple key events at the same time besides modifier keys and one other key?

My OS is Windows 10 x64, version 10.0.18362.

I think I now understand your issue - I think your problem is with the callback and the action GLFW_REPEAT.

GLFW does handle mutliple GLFW_PRESS or GLFW_RELEASE events on the same frame, but Windows only generates repeat events for one key, and only does so at a specific regular frequency not your application frame rate.

Basically GLFW_REPEAT is not intended for multiple physical key input, it’s designed for typing words like aaaaaaaaaaaarrrrrrrrrrrrrrrrggggggggghhhh. I think we should consider adding a note to the documentation to clarify this.

So if you want to know which key is down on a given frame you need to keep a state of the keys you’re interested in, or better yet just keep the state of what action you map each key to:

static in move_forwards = 0;

static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
    if( key == 'W' )
    {
        if( GLFW_RELEASE == action )
        {
            move_forwards = 0;
        }
        else
        {
            move_forwards = 1;
        }
    }
}

Edit: I’ve added an issue about the documentation: https://github.com/glfw/glfw/issues/1596

Okay, so what you’re saying is only one key each frame will be marked as GLFW_REPEAT but I will be notified when any key I held down is released?

You will receive GLFW_PRESS for every key pressed, and GLFW_RELEASE for every key released (providing the keyboard can handle that those keys pressed at the same time).

I would ignore GLFW_REPEAT actions in the key callback.

Awesome, thank you so much for your help.