Question

I was always wondering, when I press a key on my keyboard, windows sees it as KEY_DOWN event, and then my program can ask windows if there is such an event, and the windows will answer.

But then, exactly, how long does this event last? I mean, in real world we wouldn't worry about that, because our processors can process many operations per second, much faster than we can click, but what if I could click with a speed of light?

So, how exactly does KEY_DOWN is being timed?

Was it helpful?

Solution

Input events, like key-down, are buffered. When Windows receives the notification from the keyboard driver, it stores the event in the message queue. Which is owned by the thread that owns the foreground window. It will sit there however long it takes to be retrieved by your program. It needs to call GetMessage() or PeekMessage().

How long that takes is entirely unpredictable, it depends on how responsive the UI thread of the program is. Or in other words, how quickly it dispatches messages that it receives and calls GetMessage() again to retrieve the next messsage. A UI thread going comatose for a while is not unusual. Even on fast processors, a poorly written program that executes a dbase query that returns thousands of rows and waits for its result is common enough.

The buffering ensures that this doesn't cause a problem. No matter how fast the user types, his keystrokes cannot get lost.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top