When does the KeyCallback actually get called?


I really like GLFW it’s awesome and has a lot of handy stuff but using it I want to ask.
I know I can catch a key press/repeat/release by using a key callback but I wanna ask as to better improve my implementation. When is the callback invoked?
When the actual key press happens? or when the glfwPollEvents() function is called?

Thank you a lot and sorry if my question was answered before I tried looking but didn’t find it.

You should find most of your answers by carefully reading the input documenation for GLFW.

Basically, most callbacks are invoked during the call to glfwPollEvents() or glfwWaitEvents() - key presses belong in this group (though it’s not guaranteed this is the case, I believe it currently is for all OSs implemented).

Yeah I read that but the reason I asked was because I can poll the state with glfwGetKey and I know that’s just because GLFW stores all the states by itself and because of the GLFW_STICKY_KEYS mode because as I read that makes it stay pressed until glfwPollEvents is called.
That confused me a bit I am sorry but of it will stay pressed until glfwPollEvents is called then how is it that the callback will be invokd in that time?
Or does GLFW store all the states and events automatically and invokes the callbacks for processing once I call pollevents??

The sticky key mode is explained in the docs for glfwGetKey - the state of a key which is down is cleared when you call glfwGetKey() - i.e. the down state is sticky until read by glfwGetKey().

All the key events are processed during the glfwPollEvents() and the state of the keys for glfwGetKey is set then.

So presume I set it to sticky mode and processed the key during the callback. Would a later call to glfwGetKey() return GLFW_PRESS or GLFW_RELEASE?

Since only glfwGetKey() changes the sticky state of a pressed key, if the key was pressed before (or during) you called glfwPollEvents() then glfwGetKey() will return GLFW_PRESS.

To be clear - the key callback does not alter the state of the key array sampled by glfwGetKey().

Ah thank you :smiley:

And sorry for bugging you with my questions I know I they were misplaced(wrong word probably) since they were in the Docs but I didn’t understand them and trying didn’t change that result so I do apologize

No problem! Hope all is clear now :slight_smile: