# Key callback randomly called outside of glfwPollEvents() (it seems like)

**URL:** <https://discourse.glfw.org/t/key-callback-randomly-called-outside-of-glfwpollevents-it-seems-like/2940>\
**Category:** support\
**Created:** [June 13, 2025, 11:06pm UTC](https://discourse.glfw.org/t/key-callback-randomly-called-outside-of-glfwpollevents-it-seems-like/2940 "2025-06-13T23:06:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![krs](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@krs](https://discourse.glfw.org/u/krs)\
**Post date:** [June 13, 2025, 11:06pm UTC](https://discourse.glfw.org/t/key-callback-randomly-called-outside-of-glfwpollevents-it-seems-like/2940/1 "2025-06-13T23:06:37Z")

</div>

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:

```cpp
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:

```cpp
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:

```cpp
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:

```txt
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):

```txt
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.

---

<div class="post-metadata">

**Author:** ![krs](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@krs](https://discourse.glfw.org/u/krs)\
**Post date:** [June 13, 2025, 11:26pm UTC](https://discourse.glfw.org/t/key-callback-randomly-called-outside-of-glfwpollevents-it-seems-like/2940/2 "2025-06-13T23:26:18Z")

</div>

Not an glfw issue, found the mistake - I accidentally called glfwPollEvents() one more time in another place.

---

<div class="post-metadata">

**Author:** ![ypujante](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/ypujante/32/795_2.png) [@ypujante](https://discourse.glfw.org/u/ypujante)\
**Post date:** [June 16, 2025, 12:26pm UTC](https://discourse.glfw.org/t/key-callback-randomly-called-outside-of-glfwpollevents-it-seems-like/2940/3 "2025-06-16T12:26:31Z")

</div>

When you look at the [documentation](https://www.glfw.org/docs/latest/group__window.html#ga37bd57223967b4211d60ca1a0bf3c832) for `glfwPollEvents` it states this:

> Do not assume that callbacks you set will _only_ be called in response to event processing functions like this one. While it is necessary to poll for events, window systems that require GLFW to register callbacks of its own can pass events to GLFW in response to many window system function calls. GLFW will pass those events on to the application callbacks before returning.

It’s good that you found your solution, but unless I read the documentation wrong, it is not a guarantee that on all platforms the callback events will only be called during a call to `glfwPollEvents`. They could be called immediately when they happen (for example in Emscripten, that is what happens).
