Skip to content

TextField

The TextField widget provides single-line text input with cursor support, placeholder text, and keyboard navigation.

TextField is the primary widget for text input in Elaris.UI. It supports cursor movement, text editing, and placeholder text.

The text content of the field.

textField.Text = "Hello";

Placeholder text shown when the field is empty.

textField.Placeholder = "Enter your name...";

Color of the placeholder text.

textField.PlaceholderColor = Color.Gray;

The current cursor position.

int pos = textField.CursorPosition;

Raised when a key is pressed. Return true to mark as handled and prevent default behavior.

textField.KeyPress += (key) =>
{
if (key.Key == ConsoleKey.Enter)
{
// Handle Enter key
return true; // Mark as handled
}
return false; // Let default handler process it
};

Raised when a key is pressed (fires before default handling).

textField.KeyDown += (key) =>
{
// Key is being pressed
};

Raised when a key is released (fires after default handling).

textField.KeyUp += (key) =>
{
// Key was released
};

Raised when the text content changes.

textField.TextChanged += (newText) =>
{
Console.WriteLine($"Text changed to: {newText}");
};

Raised when the field receives focus.

textField.Focused += () =>
{
// Field is focused
};

Raised when the field loses focus.

textField.Blurred += () =>
{
// Field lost focus
};
  • Left/Right Arrow: Move cursor
  • Home: Move to start
  • End: Move to end
  • Backspace: Delete character before cursor
  • Delete: Delete character after cursor
  • Enter: Move cursor to end
  • Tab: Navigate to next focusable widget
var textField = new TextField("Enter your name...")
{
X = 10,
Y = 5,
Width = 40,
Height = 1,
ForegroundColor = Color.White,
BackgroundColor = ColorHelper.FromRgb(30, 30, 30),
PlaceholderColor = Color.Gray
};
textField.KeyPress += (key) =>
{
if (key.Key == ConsoleKey.Enter)
{
Console.WriteLine($"Entered: {textField.Text}");
textField.Text = string.Empty;
return true;
}
return false;
};