Events
From SDL.NET
Introduction
One of the primary strengths of SDL.NET is input handling. SDL.NET provides a simple API for handling many different kind of input events. The most common of these are keyboard, mouse and tick events. This tutorial will demo the basics of using these input events.
Basic Event Processing
The Events class contains many different events that an application can use. Most applications will want to capture keyboard and mouse input by a user. Many apps will also want to have a ticker running to perform timed actions such as blitting a Surface to the screen.
The Events class contains these relevant events:
KeyboardUp KeyboardDown MouseMotion MouseButtonDown MouseButtonUp Tick Quit
An application can attach an EventHandler to each of these events to process mouse or keyboard input. The EventHandlers should be instantiated when the application is initialized.
To use these event handlers, remember to include
using SdlDotNet.Input;
C# code:
Events.KeyboardDown += new EventHandler<KeyboardEventArgs>(this.OnKeyboardDown); Events.MouseMotion += new EventHandler<MouseMotionEventArgs>(this.OnMouseMotion); Events.MouseButtonDown += new EventHandler<MouseButtonEventArgs>(this.OnMouseButtonDown); Events.Tick += new EventHandler<TickEventArgs>(this.OnTick); Events.Quit += new EventHandler<QuitEventArgs>(this.OnQuit);
Start the event processing loop in main part of the program
Events.Run();
Create private methods that will process the events.
//Handles keyboard events. The 'Escape' and 'Q'keys will cause the app to exit
private void OnKeyboardDown(object sender, KeyboardEventArgs e)
{
if (e.Key == Key.Escape || e.Key == Key.Q)
{
Events.QuitApplication();
}
}
// This will set the position of a custom cursor to blit to the screen
private void OnMouseMotion(object sender, MouseMotionEventArgs e)
{
position.X = e.X;
position.Y = e.Y;
}
// This changes the rendering of an OpenGL heightmap when the mouse button is pressed.
private void OnMouseButtonDown(object sender, MouseButtonEventArgs e)
{
bRender = !bRender;
}
//A ticker is running to update the screen constantly.
//This method will fill the screen with black to clear it out.
//Then it will Blit the surface to the screen.
//Then it will refresh the screen and display it.
private void OnTick(object sender, TickEventArgs args)
{
screen.Fill(Color.Black);
screen.Blit(surface);
screen.Flip();
}
Conclusion
Events are often used in the main program, but they are often used by Sprites as well. Consult the Sprites demo to see how Sprites and SpriteCollections consume events to create motion and react to use input.

