I have a simple wrapper around key events. I have a vector that I populate in the key callback with whatever key event happened, I then check it in the middle of the frame for inputs, and clear it at the end of the frame. My main loop looks something like this:
while(!glfwWindowShouldClose(w)) {
glfwPollEvents();
if (Input::isKeyJustPressed(GLFW_KEY_G)) printf("G\n");
// Drawing...
Input::clear();
}
I record key events in the key callback like this:
void Input::keyCallback(
GLFWwindow* window, int key, int scancode, int action, int mods)
{
keyKeyboardEvents.push_back({key, scancode, action, mods});
printf("-------------------------------------\n");
printf("Recording key: %d, action: %d\n", key, action);
}
And in Input::clear()
I do:
keyKeyboardEvents.clear();
For some reason Input::isKeyJustPressed(GLFW_KEY_G)
returns true
around 1 out of 10 times. If I populate everywhere with prints I get this situation:
Checking for key: 71
Checking for key: 71
Checking for key: 71
Checking for key: 71
-------------------------------------
Recording key: 71, action: 0
Clearing key: 71, action: 0
Checking for key: 71
Checking for key: 71
Checking for key: 71
Checking for key: 71
Checking for key: 71
As you can see - somehow the event is getting erased from the vector before it is checked, which doesn’t make sense, considering an order of operations in my code. The only reasonable suspicion I have is that callback somehow is not getting called in glfwPollEvents()
.
And once in a time I get expected behavior, where order makes sense(around once in every 5-20 presses):
Checking for key: 71
Checking for key: 71
Checking for key: 71
Checking for key: 71
-------------------------------------
Recording key: 71, action: 1
Checking for key: 71
G
Clearing key: 71, action: 1
Checking for key: 71
Checking for key: 71
Checking for key: 71
Checking for key: 71
The strangest thing is that I used this little wrapper in my other project on my other pc, and it worked correctly. I literally look at my old project code where everything worked nicely exactly like this. Unfortunately, I can’t build this old project on my current machine to check if the issue persists, and it is specific to this pc, because its too complicated.
My current machine: Minimal Debian 12 Bookworm running X server.