user565739
user565739

Reputation: 1332

Calling GetKeyState in a tight loop, why doesn't it work?

In Charles Petzold's book "Programming Windows", he mentioned the following:

"Be careful with GetKeyState. It is not a real-time keyboard status check. Rather it reflects the keyboard status up to and including the current message being processed."

"Do not do while(GetKeyState(VK_F1) >= 0);", it is guaranteed to hang your program.

I don't understand these at all. Could someone give an explanation for these two facts, please.

Upvotes: 1

Views: 491

Answers (1)

David Heffernan
David Heffernan

Reputation: 613063

Every time you read a queued keyboard message, for example by calling GetMessage, the OS updates private keyboard state data associated with the calling thread. When you call GetKeyState that private keyboard state data is used to determine the returned key state. Thus, so long as you don't read another queued message, GetKeyState will always return the same value.

Upvotes: 4

Related Questions