Skip to content

Text Input

rizuki edited this page Nov 30, 2026 · 2 revisions

Text input

TextInput is a single-line text field. It draws a box with the text, a caret and an optional placeholder, and reads the keyboard while it has focus. The editing state lives in a CC_TextEdit that you own, so it survives across frames and can be driven without a window.

CC_TextEdit

Field Meaning
text UTF-8 content, always NUL terminated. Capacity is CC_TEXT_EDIT_CAP (256) bytes.
len Bytes in text, without the NUL.
cursor Byte offset on a code point boundary.
max_len Byte limit. 0 means CC_TEXT_EDIT_CAP - 1.
all_selected True after select-all. It is the only selection there is.

Zero-initialise it, then call CC_TextEditSet(&e, "initial") to give it content. It truncates at max_len on a code point boundary and puts the cursor at the end.

CC_TextEditApply

bool CC_TextEditApply(CC_TextEdit *e, CC_TextEditOp op, const char *text) applies one edit and returns true when the content changed. text is read only by CC_EDIT_INSERT.

Op Effect
CC_EDIT_INSERT Insert text at the cursor, or replace everything when select-all is active. Stops at max_len, never inside a code point.
CC_EDIT_BACKSPACE Delete the code point before the cursor, or everything when select-all is active.
CC_EDIT_DELETE Delete the code point after the cursor.
CC_EDIT_LEFT, CC_EDIT_RIGHT Move one code point. Clears the selection.
CC_EDIT_HOME, CC_EDIT_END Move to the start or end. Clears the selection.
CC_EDIT_SELECT_ALL Select the whole text.

TextInput options

int TextInput(id, &edit, ...) takes these designated initialisers.

Option Default
.sizing Grow() by Fixed(40)
.padding PadSymmetric(12, 10)
.background none
.border none. A border of 1px is drawn when the colour has alpha.
.borderFocused .border
.textColor the global font colour
.placeholderColor .textColor at 45% alpha
.caretColor .textColor
.selectionColor none. Background of select-all text.
.cornerRadius 0
.fontId, .fontSize the global font and size
.placeholder none. Shown while the field is empty.

Events

The return value is a bitmask of CC_TextInputEvent.

Event When
CC_TEXT_INPUT_CHANGED The content changed this frame.
CC_TEXT_INPUT_SUBMITTED Enter was pressed while focused.
CC_TEXT_INPUT_FOCUSED A click gave the field focus.
CC_TEXT_INPUT_BLURRED A click outside, Escape or Enter took focus away.

Focus rules

Clicking the field takes keyboard focus. Clicking elsewhere, Escape or Enter releases it. While focused it reads typed characters, Backspace, Delete, Left, Right, Home, End, Ctrl+A and Ctrl+V. CC_TextInputFocused("id") reports focus and CC_TextInputBlur() drops it. CC_TextInputFocus("id") gives a field focus from code, for one that just appeared: the next CC_TextInput with that id reports CC_TEXT_INPUT_FOCUSED once and reads keys. CC_Init turns off raylib's default Escape-closes-window key so Escape can blur a field.

A typical pattern

static CC_TextEdit city;

CC_TextEditSet(&city, "Jakarta");   // once

// every frame, inside a layout
int ev = TextInput("city", &city, .placeholder = "City",
                   .border = ColorHex(0x555555),
                   .borderFocused = ColorHex(0x4488FF));
if (ev & CC_TEXT_INPUT_SUBMITTED) search(city.text);

Gotchas

  • Only one field has focus at a time.
  • Ids hash by content, so two fields with the same id share focus.
  • There is no IME support and no mouse caret placement or drag selection.
  • Pasting stops at the first newline.
  • In a headless build the field lays out the same elements but reads no keys.

Clone this wiki locally