This post focuses on Silverlight 2 Beta 1.
I was following along with the Asteroids tutorial from Bill Reiss' Silverlight Games 101 blog. I got to the post where he explained how to use a custom KeyHandler framework to handle the keyboard events. He was illustrating how to use a utility class to keep track of the KeyDown, KeyUp, and LostFocus events. This class worked in tandem with a game loop mechanism to handle the "action." By action, I mean things like moving the ship and collision detection, in response to user keyboard input.
My first reaction was that this framework was overkill. It seemed like a lot of work just to handle keyboard events. I tried doing it with a simpler method (like in Mike Snow's tutorial), calling the "action" code in the KeyDown event:
private void parentControl_KeyDown(object sender, KeyEventArgs e) {
if (e.Key == Key.A) {
ship.Rotate(ShipRotationDirection.CounterClockwise);
vectorship.RotationAngle -= 6;
}
else if (e.Key == Key.D) {
ship.Rotate(ShipRotationDirection.Clockwise);
vectorship.RotationAngle += 6;
}
if (e.Key == Key.W) {
ship.Thrust();
vectorship.Thrust();
}
}
This simple code worked, but was not nearly as responsive as Bill's framework that used a game loop and a KeyHandler class. My simple approach also did not handle multiple simultaneous keypresses very well. For games, it was definitely not good enough.
I was further convinced when Andy Beaulieu's Silverlight Rocks used a very similar strategy. In fact, I was surprised to see that just Mike Snow's tutorial used simple keyboard event handlers. Maybe his game will be turn-based and not an action game.
In summary, using simple event handlers will usually be good enough. However, applications that depend on fast response times, like action games, will require a key handler and game loop framework like the one Bill Reiss shares in his tutorial.