I have this function which should track how many keys have been pressed, but it only tracks the keys pressed when the window is selected. If I have a different window selected, it doesn't get the message. How can I change this?
Although generally, you REALLY don't want to receive key messages if your window currently isn't selected. Imagine if a user writes something in a text editor or something, which suddenly ends up doing something to your program.
Yes, this will happen if u'r calling GetAsyncKeyState too often. Also, since u'r using windows messages, sleep() should almost never be used...
What u are looking for then is a global hook that will capture all WM_KEYUP messages.
Fair warning: this kind of hook is something that virus defenders will often flag as malicious and with good reason.
There's lots of info online on how to do this. I suggest looking there.