# Getting timestamp of key events

**URL:** <https://discourse.glfw.org/t/getting-timestamp-of-key-events/910>\
**Category:** support\
**Created:** [May 8, 2017, 5:36pm UTC](https://discourse.glfw.org/t/getting-timestamp-of-key-events/910 "2017-05-08T17:36:47Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Overblade](https://avatars.discourse-cdn.com/v4/letter/o/b2d939/32.png) [@Overblade](https://discourse.glfw.org/u/Overblade)\
**Post date:** [May 8, 2017, 5:36pm UTC](https://discourse.glfw.org/t/getting-timestamp-of-key-events/910/1 "2017-05-08T17:36:48Z")

</div>

I really like glfw because it’s a small but powerful library 😄.

In my game, I currently buffer key events to pass them in fixed time intervals for updating entities. Although I use a key callback, I cannot get the time when the event was fired by the native window system.

Is there a way to access a timestamp? Or do I need to create a workaround? Thanks in advance.

---

<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:** [May 8, 2017, 6:01pm UTC](https://discourse.glfw.org/t/getting-timestamp-of-key-events/910/2 "2017-05-08T18:01:01Z")

</div>

There is no timestamp with the callback, as the OS may not provide one (for example WM\_KEYDOWN on Windows doesn’t [https://msdn.microsoft.com/en-us/library/windows/desktop/ms646280(v=vs.85).aspx](https://msdn.microsoft.com/en-us/library/windows/desktop/ms646280(v=vs.85).aspx)).

If you need one, just generate a timestamp in your callback.

---

<div class="post-metadata">

**Author:** ![Overblade](https://avatars.discourse-cdn.com/v4/letter/o/b2d939/32.png) [@Overblade](https://discourse.glfw.org/u/Overblade)\
**Post date:** [May 8, 2017, 6:47pm UTC](https://discourse.glfw.org/t/getting-timestamp-of-key-events/910/3 "2017-05-08T18:47:39Z")

</div>

Thanks for your reply.

But how would I generate a timestamp in my callback? Callbacks are called sequentially for each event, but I cant know when the event was posted by the OS, or am I missing something?

---

<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:** [May 8, 2017, 6:55pm UTC](https://discourse.glfw.org/t/getting-timestamp-of-key-events/910/4 "2017-05-08T18:55:33Z")

</div>

The only timestamp you can use here is the time the callback was called - i.e. call a function like [glfwGetTime() or glfwGetTimerValue()](http://www.glfw.org/docs/latest/group__input.html#gaa6cf4e7a77158a3b8fd00328b1720a4a).

---

<div class="post-metadata">

**Author:** ![Overblade](https://avatars.discourse-cdn.com/v4/letter/o/b2d939/32.png) [@Overblade](https://discourse.glfw.org/u/Overblade)\
**Post date:** [May 8, 2017, 7:46pm UTC](https://discourse.glfw.org/t/getting-timestamp-of-key-events/910/5 "2017-05-08T19:46:24Z")

</div>

So there is no platform-independent way of getting the real time the user pressed a key?

---

<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:** [May 8, 2017, 10:11pm UTC](https://discourse.glfw.org/t/getting-timestamp-of-key-events/910/6 "2017-05-08T22:11:38Z")

</div>

If you take a look at the [events.c](https://github.com/glfw/glfw/blob/master/tests/events.c) test you’ll see glfwPollEvents used to poll and time key presses and other events.

If you want the poll to return after a finite amount of time, you can use the [glfwPollEventsTimeout function](http://www.glfw.org/docs/latest/group__window.html#ga605a178db92f1a7f1a925563ef3ea2cf) or you can post a null event from another thread using [glfwPostEmptyEvent](http://www.glfw.org/docs/latest/group__window.html#gab5997a25187e9fd5c6f2ecbbc8dfd7e9)

---

<div class="post-metadata">

**Author:** ![Overblade](https://avatars.discourse-cdn.com/v4/letter/o/b2d939/32.png) [@Overblade](https://discourse.glfw.org/u/Overblade)\
**Post date:** [May 9, 2017, 2:32pm UTC](https://discourse.glfw.org/t/getting-timestamp-of-key-events/910/7 "2017-05-09T14:32:10Z")

</div>

Oh that’s a nice approach! Thank you very much! 😄

---

<div class="post-metadata">

**Author:** ![ocornut](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/ocornut/32/1072_2.png) [@ocornut](https://discourse.glfw.org/u/ocornut)\
**Post date:** [March 30, 2026, 9:21am UTC](https://discourse.glfw.org/t/getting-timestamp-of-key-events/910/8 "2026-03-30T09:21:01Z")

</div>

Thread necromancy time!

> There is no timestamp with the callback, as the OS may not provide one (for example WM\_KEYDOWN on Windows doesn’t

FYI i was investigating a similar topic for dear imgui, trying to find how to associate time to IO events. It turns out Win32 API has `GetMessageTime()` which I verified works for WM\_KEYDOWN, WM\_LBUTTONDOWN etc.  
See details:

> **[GetMessageTime function (winuser.h) - Win32 apps](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getmessagetime)**
>
> Retrieves the message time for the last message retrieved by the GetMessage function.

Other “external” source of information for Win32 message include `GetMessageExtraInfo()`.

The issue for me is to resolve issues with long frame times where events might be queuing up.  
For reference SDL2/SDL3 carry a `Timestamp` value in inputs events.

---

<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:** [March 30, 2026, 2:24pm UTC](https://discourse.glfw.org/t/getting-timestamp-of-key-events/910/9 "2026-03-30T14:24:20Z")

</div>

That’s good to know.
