Keyboard Input

odl also contains simple input handling. By calling Graphics.Update(), you are automatically also updating all input handling.

circle-info

For information on how to receive free text from the keyboard (for text boxes, for instance), see the Free Text Input section.

Input handling can be done in the main program loop, or inside the OnTick event of a Window object.

using System;
using odl;

namespace TestNamespace
{
    class Program
    {
        public static void Main(string[] args)
        {
            Graphics.Start();

            Window w = new Window();
            w.Initialize();
            w.Show();

            w.OnTick += delegate (BaseEventArgs e)
            {
                if (Input.Trigger(SDL2.SDL.SDL_Keycode.SDLK_RETURN))
                {
                    Console.WriteLine("Return from the window's OnTick event!");
                }
            };

            while (Graphics.CanUpdate())
            {
                Graphics.Update();
                if (Input.Trigger(SDL2.SDL.SDL_Keycode.SDLK_RETURN))
                {
                    Console.WriteLine("Return from the game loop!");
                }
            }

            Graphics.Stop();
        }
    }
}

Aside from Input.Trigger(), which returns true once when the specified key is pressed down, there is also Input.Press(), which returns true while the specified key is being held down.

For anything more sophisticated than that, such as repeated input, I suggest writing your own wrapper and implementation. You could use SDL2's SDL_GetTicks() function for that, for instance. Or you could use C#'s own Stopwatch class, which tracks time in nanoseconds.

Last updated