# When does the KeyCallback actually get called?

**URL:** <https://discourse.glfw.org/t/when-does-the-keycallback-actually-get-called/713>\
**Category:** support\
**Created:** [July 29, 2016, 4:41pm UTC](https://discourse.glfw.org/t/when-does-the-keycallback-actually-get-called/713 "2016-07-29T16:41:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![MasterAbdoTGM50](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/masterabdotgm50/32/50_2.png) [@MasterAbdoTGM50](https://discourse.glfw.org/u/MasterAbdoTGM50)\
**Post date:** [July 29, 2016, 4:41pm UTC](https://discourse.glfw.org/t/when-does-the-keycallback-actually-get-called/713/1 "2016-07-29T16:41:05Z")

</div>

Hi,

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.

---

<div class="post-metadata">

**Author:** ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)\
**Post date:** [July 29, 2016, 4:59pm UTC](https://discourse.glfw.org/t/when-does-the-keycallback-actually-get-called/713/2 "2016-07-29T16:59:47Z")

</div>

You should find most of your answers by carefully reading the [input documenation for GLFW](http://www.glfw.org/docs/latest/input_guide.html).

Basically, most callbacks are invoked during the call to [glfwPollEvents()](http://www.glfw.org/docs/latest/group__window.html#ga37bd57223967b4211d60ca1a0bf3c832) or [glfwWaitEvents()](http://www.glfw.org/docs/latest/group__window.html#ga554e37d781f0a997656c26b2c56c835e) - key presses belong in this group (though it’s not guaranteed this is the case, I believe it currently is for all OSs implemented).

---

<div class="post-metadata">

**Author:** ![MasterAbdoTGM50](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/masterabdotgm50/32/50_2.png) [@MasterAbdoTGM50](https://discourse.glfw.org/u/MasterAbdoTGM50)\
**Post date:** [July 29, 2016, 5:13pm UTC](https://discourse.glfw.org/t/when-does-the-keycallback-actually-get-called/713/3 "2016-07-29T17:13:58Z")

</div>

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??

---

<div class="post-metadata">

**Author:** ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)\
**Post date:** [July 29, 2016, 5:34pm UTC](https://discourse.glfw.org/t/when-does-the-keycallback-actually-get-called/713/4 "2016-07-29T17:34:10Z")

</div>

[The sticky key mode is explained in the docs for glfwGetKey](http://www.glfw.org/docs/latest/group__input.html#gadd341da06bc8d418b4dc3a3518af9ad2) - 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.

---

<div class="post-metadata">

**Author:** ![MasterAbdoTGM50](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/masterabdotgm50/32/50_2.png) [@MasterAbdoTGM50](https://discourse.glfw.org/u/MasterAbdoTGM50)\
**Post date:** [July 29, 2016, 6:49pm UTC](https://discourse.glfw.org/t/when-does-the-keycallback-actually-get-called/713/5 "2016-07-29T18:49:25Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)\
**Post date:** [July 29, 2016, 7:40pm UTC](https://discourse.glfw.org/t/when-does-the-keycallback-actually-get-called/713/6 "2016-07-29T19:40:52Z")

</div>

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().

---

<div class="post-metadata">

**Author:** ![MasterAbdoTGM50](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/masterabdotgm50/32/50_2.png) [@MasterAbdoTGM50](https://discourse.glfw.org/u/MasterAbdoTGM50)\
**Post date:** [July 29, 2016, 7:54pm UTC](https://discourse.glfw.org/t/when-does-the-keycallback-actually-get-called/713/7 "2016-07-29T19:54:57Z")

</div>

Ah thank you 😃

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

---

<div class="post-metadata">

**Author:** ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)\
**Post date:** [July 29, 2016, 8:07pm UTC](https://discourse.glfw.org/t/when-does-the-keycallback-actually-get-called/713/8 "2016-07-29T20:07:01Z")

</div>

No problem! Hope all is clear now 🙂
