Getting timestamp of key events


#1

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.


#2

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.


#3

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?


#4

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


#5

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


#6

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


#7

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