In this short snippet, I will show you a Win32 function that checks whether a button is pressed or not. The good thing is that you don't need to handle the WM_KEYDOWN message. The second advantage is that it is really easy to use and that it can be used everywhere in the code. Here is the function-definition:
vKey is the virtual key code of the keyboard or mouse button you want to test. GetAsyncKeyState(..) returns true if this button is pressed and false if not. I always use GetAsyncKeyState(..) for my games and demos because it can be used simply like this without a long message handle. Here is a little example how to use it:
In this function, we will check what keys are down. You could now use this function in your main loop or somewhere else in your code. Here is a list of the most important virtual key codes:
|Virtual Key Code||Corresponding key on the keyboard|
|VK_LBUTTON||Left Mouse Button|
|VK_RBUTTON||Right Mouse Button|
That's it! If you run into problems, simply m@il me. Happy Coding!