Skip to content

Text fields

Use <input> and <textarea> when the player types a name, a search or a note, and a view model must see the text.

The TextFields sample has an <input> and a <textarea> that show the text while it is typed and the text that was committed when the field was left. Text fields behave as in Chrome. Run it by choosing TextFields in the samples list in the map L_ArcRmlSamples (Samples).

The document

TextFields.rml
<input id="tf-input" class="tf-field" type="text" placeholder="Type a name"
    attr:value="tf.Live"
    on:input="call('tf', 'SetLive', ev.value)"
    on:change="call('tf', 'Commit', ev.value)"/>
<textarea id="tf-notes" class="tf-field tf-notes" placeholder="Type some notes"
    attr:value="tf.NotesLive"
    on:input="call('tf', 'SetNotesLive', ev.value)"
    on:change="call('tf', 'CommitNotes', ev.value)"></textarea>
TextFields/ArcRmlSamplesTextFields.cpp
void UArcRmlSamplesTextFieldsViewModel::SetLive(const FString& Value)
{
    UE_MVVM_SET_PROPERTY_VALUE(Live, Value);
}

void UArcRmlSamplesTextFieldsViewModel::Commit(const FString& Value)
{
    UE_MVVM_SET_PROPERTY_VALUE(Committed, Value);
    UE_MVVM_SET_PROPERTY_VALUE(CommitCount, CommitCount + 1);
}
  • input fires for every edit (typing, delete, paste, cut), so Live follows the field. change fires when focus leaves the field, or on Enter in the <input>, when the text differs from the one at focus or at the last change. Enter in the <textarea> inserts a line break. Both events carry ev.value.
  • Show the model's text with attr:value, as the sample does; :value sets the same value attribute. attr:value never writes back, so the sample's on:input calls a function that stores each edit; :value on a BlueprintReadWrite field writes the text on change (Forms).
  • A value written through attr:value fires no event. On a focused field the write does not become the committed value, so change still fires when the field is left; echoing the field's own text writes the same value and the caret stays.
  • placeholder="..." is shown while the field is empty and styled with ::placeholder.

Run it

Play in the map L_ArcRmlSamples and choose TextFields in the samples list. Click the name field and type Ann. Live: follows every key and reads Ann; Committed: stays empty and Commits so far: reads 0. Press Enter: Committed: reads Ann and Commits so far: reads 1. Click the notes field, type a line and click the name field: Notes live: followed the typing, and now Notes committed: shows the text and Commits so far: reads 2.

Typing and focus

A text field types (caret, text input active) from the moment it takes focus. Typing mode means the field takes keys as text. With a gamepad or the keyboard:

Input In a typing field On a focused field that is not typing
Gamepad back Ends typing: change fires, the field keeps focus, the caret goes The document's Back (here it closes the screen)
Gamepad accept, Enter Enter commits an <input>; in a <textarea> a line break Types again; no click, no change
Esc Nothing happens to the text or focus; the document gets an ordinary keydown whose ev.key is Escape Back
Arrows, D-pad Move the caret, at its ends move focus Move focus

After gamepad back the field is an ordinary focused control: the D-pad moves focus from it, and a second gamepad back is the screen's Back (Navigation). A mouse press, a printable key, Backspace or Del on it types again.

Limits

  • A gamepad press that starts focus navigation ends typing, as gamepad back does.
  • Left and Right at the start or end of the text move focus, and so do Up and Down on the first or last line.