Skip to main content

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​

FieldMeaning
textUTF-8 content, always NUL terminated. Capacity is CC_TEXT_EDIT_CAP (256) bytes.
lenBytes in text, without the NUL.
cursorByte offset on a code point boundary.
max_lenByte limit. 0 means CC_TEXT_EDIT_CAP - 1.
all_selectedTrue 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.

OpEffect
CC_EDIT_INSERTInsert text at the cursor, or replace everything when select-all is active. Stops at max_len, never inside a code point.
CC_EDIT_BACKSPACEDelete the code point before the cursor, or everything when select-all is active.
CC_EDIT_DELETEDelete the code point after the cursor.
CC_EDIT_LEFT, CC_EDIT_RIGHTMove one code point. Clears the selection.
CC_EDIT_HOME, CC_EDIT_ENDMove to the start or end. Clears the selection.
CC_EDIT_SELECT_ALLSelect the whole text.

TextInput options​

int TextInput(id, &edit, ...) takes these designated initialisers, which fill a CC_TextInputOpts. The macro calls int CC_TextInput(const char *id, CC_TextEdit *edit, CC_TextInputOpts opts), which you can call directly when you build the options at runtime.

OptionDefault
.sizingGrow() by Fixed(40)
.paddingPadSymmetric(12, 10)
.backgroundnone
.bordernone. A border of 1px is drawn when the colour has alpha.
.borderFocused.border
.textColorthe global font colour
.placeholderColor.textColor at 45% alpha
.caretColor.textColor
.selectionColornone. Background of select-all text.
.cornerRadius0. A single float for all four corners.
.fontId, .fontSizethe global font and size
.placeholdernone. Shown while the field is empty.

Events​

The return value is a bitmask of CC_TextInputEvent.

EventWhen
CC_TEXT_INPUT_CHANGEDThe content changed this frame.
CC_TEXT_INPUT_SUBMITTEDEnter was pressed while focused.
CC_TEXT_INPUT_FOCUSEDA click gave the field focus.
CC_TEXT_INPUT_BLURREDA 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.