On Windows, there are several APIs to process input from the user from the keyboard, mouse, and controllers.
To obtain input from the keyboard and mouse:
To obtain input from controllers:
When the user presses or releases a key when a window has keyboard focus, the window receives the WM_KEYDOWN
and WM_KEYUP
messages.
The wParam
contains the virtual-key code of the key (VK
constants).
We can handle each key with a switch statement.
case WM_KEYDOWN:
switch (wParam)
{
// Handle pressed keys
// ...
}
return 0;
case WM_KEYUP:
switch (wParam)
{
// Handle released keys
// ...
}
return 0;
For example, we handle the Escape key and terminate the application.
case VK_ESCAPE:
PostQuitMessage(0);
break;
To be able to handle the event outside of the window procedure, we can: