C i p h e r s o f t w a r e

GetAsyncKeyState()

GetAsyncKeyState() is a great way to check for keypresses in your game. GetAsyncKeyState() returns the current state of the given key. Lets say you want to move your player to the right when the user presses the right arrow key: if(GetAsyncKeyState(VK_RIGHT)) player++;

Thats basicly all there is to it. You'll see that we're passing VK_RIGHT as an argument. VK_RIGHT is a predefined constant representing the keys keycode. Here's a list of the most common predefined virtual keycodes:


VK_LBUTTON
VK_RBUTTON
VK_CANCEL
VK_MBUTTON
VK_XBUTTON1
VK_XBUTTON2
VK_BACK
VK_TAB
VK_CLEAR
VK_RETURN
VK_SHIFT
VK_CONTROL
VK_MENU
VK_PAUSE
VK_CAPITAL
VK_ESCAPE
VK_SPACE
VK_PRIOR
VK_NEXT
VK_END
VK_HOME
VK_LEFT
VK_UP
VK_RIGHT
VK_DOWN
VK_SELECT
VK_PRINT
VK_EXECUTE
VK_SNAPSHOT
VK_INSERT
VK_DELETE
VK_HELP
VK_LWIN
VK_RWIN
VK_APPS
VK_NUMPAD0
...
VK_NUMPAD9
VK_MULTIPLY
VK_ADD
VK_SEPARATOR
VK_SUBTRACT
VK_DECIMAL
VK_DIVIDE
VK_F1
...
VK_F12
VK_NUMLOCK
VK_SCROLL
VK_LSHIFT
VK_RSHIFT
VK_LCONTROL
VK_RCONTROL
VK_LMENU
VK_RMENU

As you may have noticed, there are no VK_A's or VK_B's. For the alpha keys, you just need to use its decimal keycode value.

The other slow and tedious method would be to trap all WM_KEYUP's and WM_KEYDOWN's in your message loop. I've been told that this method is much slower than GetAsyncKeyState().