Free Text Input

Free text input refers to being able to turn any key pressed on the keyboard into text, without having to check for every single possible combination via Input.Trigger or something similar. As such, using the free text input method to obtain a user-inputted string of text supports any language, any keyboard, and any character. The only limitation, really, is what characters the font you're using supports.

Free text input must be started with Input.StartTextInput(), and ended with Input.StopTextInput(). Whenever the user inserts a key, the active window's OnTextInput event will be called with a special TextEventArgs parameter. Although backspace and delete aren't official keys (as they can't be displayed), the TextEventArgs object contains two fields to handle backspace and delete. Enter (often called Return instead) is a displayable character, however, and is represented by \n.(as is standard in programming). It is up to you, the developer, to handle or not to handle newlines.

Below is an example of simple free text management.

result.mp4
using System;
using odl;

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

            Window win = new Window();
            win.Initialize(true, true);

            Viewport vp = new Viewport(0, 0, win.Width, win.Height);

            Sprite s = new Sprite(vp);
            s.Bitmap = new Bitmap(vp.Width, vp.Height);
            s.Bitmap.Font = Font.Get("arial", 24);
            string text = "";

            win.Show();

            win.OnTextInput += delegate (TextEventArgs e)
            {
                string oldtext = text;
                if (e.Backspace)
                {
                    if (text.Length > 0) text = text.Remove(text.Length - 1);
                }
                else if (e.Delete) { }
                else
                {
                    text += e.Text;
                }
                if (text != oldtext)
                {
                    s.Bitmap.Unlock();
                    s.Bitmap.Clear();
                    s.Bitmap.DrawText(text, 5, 20, Color.WHITE);
                    s.Bitmap.Lock();
                }
            };

            Input.StartTextInput();

            while (Graphics.CanUpdate())
            {
                Graphics.Update();
            }

            Input.StopTextInput();

            if (!vp.Disposed) vp.Dispose();

            Graphics.Stop();
        }
    }
}

Last updated