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.
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 callGetKeyStatethat private keyboard state data is used to determine the returned key state. Thus, so long as you don’t read another queued message,GetKeyStatewill always return the same value.