Getting timestamp of key events

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

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.

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

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

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?

The only timestamp you can use here is the time the callback was called - i.e. call a function like glfwGetTime() or glfwGetTimerValue().

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

If you take a look at the 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 or you can post a null event from another thread using glfwPostEmptyEvent

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

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:

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.

That’s good to know.