Reading the keyboard

Getting input from the keyboard under Windows is easy. The system notifies the application via the WM_KEYDOWN and WM_KEYUP messages. All we have to do is respond to these messages. We will insert some code in the window procedure and get the key that was pressed or released.

case WM_KEYDOWN: // Update Keyboard Buffers For clf_keys Pressed
	if ((wParam >= 0) && (wParam <= 255)) // Is Key (wParam) In A Valid Range?
	{
		keyDown [wParam] = true; // Set The Selected Key (wParam) To True
		return 0; // Return
	}
	break; // Break

case WM_KEYUP: // Update Keyboard Buffers For clf_keys Released
	if ((wParam >= 0) && (wParam <= 255)) // Is Key (wParam) In A Valid Range?
	{
		keyDown [wParam] = false; // Set The Selected Key (wParam) To False
	return 0; // Return
	}
	break; // Break

We have already introduced a global array of boolean values to hold the status of each key. When a key is pressed, we set its value to true and when it is released we set it to false. Then in the frame_move function we check the values of the keys we want and take appropriate actions.

void frame_move(float fElapsed)
{
	// rotate 30 degrees per second
	fRotation += fAngularSpeed*fElapsed;
	if (keyDown['L']) // left
	{
		fAngularSpeed += 1.f;
	}
	if (keyDown['R']) // right
	{
		fAngularSpeed -= 1.f;
	}
}

You can download the code for this tutorial from