UI Keyboard
Updated
An on-screen letter keyboard.
- Type
ent-ui-keyboard- Script class
UIKeyboardEntity- Family
- UI
Overview
Section titled “Overview”A UI Keyboard is a keyboard drawn inside the scene, pressed with a mouse, a finger or an XR controller ray. Its keys type into whichever UI Input has focus, so one keyboard serves every field in the scene.
You often do not need to place one: when a field gains focus in a headset without a system keyboard, or on any device when the field has virtualKeyboard: true, the runtime opens its own shared keyboard. Place a UI Keyboard yourself for a keyboard that stays in a fixed place, such as on a kiosk or under a sign-in panel. It can be a root in world space at its transform, or nested in a panel.
The keyboard carries one of two layouts:
- UI Keyboard QWERTY (
comp-ui-keyboard-qwerty, what a new keyboard starts with): letters, digits, backspace, space and enter. Shift capitalizes the next letter, ?123 opens a page of symbols and ABC returns to letters. - UI Keyboard Numeric (
comp-ui-keyboard-numeric):0to9,.,-, backspace and enter.
To switch layouts, add the other layout component; it replaces the current one. Scripts reach either layout as UIKeyboardComponent.
A new keyboard already has a Signal Emitter on the signal ui:keyboard, and so does the one the runtime opens. It reports UIKeyboard::press with { key } for every key that types (the character, or backspace, space or enter; Shift and page switches send nothing), and UIKeyboard::show and UIKeyboard::hide.
The props that matter most:
color: the key hover colour.opacity: the alpha of the keyboard’s fill.paddingandmargin: the space inside and around the keyboard, in layout pixels (px).
From a script, show(), hide() and isShown() on the layout component show, hide and query the keyboard.
Set it up
Section titled “Set it up”- Add a UI Keyboard where it should stay: at the scene root or under a 3D object for a free-standing keyboard, or under a UI Panel. See Scenes and hierarchy.
- Keep the QWERTY layout, or add UI Keyboard Numeric for a keypad.
- Place a UI Input nearby; pressing it gives it focus, and the keyboard types into it.
- Listen on
ui:keyboardif a script should react to keys.
Examples
Section titled “Examples”Key click sounds
Section titled “Key click sounds”Goal: play a short click for every key pressed on any in-scene keyboard.
#pragma lifecycle(startup)
// Scene: an Audio entity named 'Key Click' that plays a short click (repeat: 'once').function startup() { const click = findEntity('Key Click').getComponent(AudioComponent);
on('ui:keyboard', (event) => { if (event.eventType === EventType.UIKeyboard.Press) click.play(); });}How it works:
- Every keyboard entity reports on
ui:keyboardwithout any setup, including the one the runtime opens by itself. UIKeyboard::pressarrives for each key that types, so Shift and page switches stay silent.
A keypad that shows and hides
Section titled “A keypad that shows and hides”Goal: a fixed numeric keypad on a kiosk appears when a button is pressed and hides once enter is pressed.
#pragma lifecycle(startup)
// Scene: a UI Keyboard named 'Keypad' with the UI Keyboard Numeric layout,// a UI Input named 'Amount', and a UI Button named 'Enter Amount' (Signal Emitter: 'kiosk:amount').function startup() { const keypad = findEntity('Keypad').getComponent(UIKeyboardComponent); findEntity('Amount').getComponent(UIInputComponent).setProps({ text: 'Amount', type: 'number' }); keypad.hide();
on('kiosk:amount', (event) => { if (event.eventType === EventType.UIButton.Press && !keypad.isShown()) keypad.show(); });
on('ui:keyboard', (event) => { if (event.eventType === EventType.UIKeyboard.Press && event.data.key === 'enter') keypad.hide(); });}How it works:
getComponent(UIKeyboardComponent)returns the keyboard’s layout, whichever one it carries.hide()at start keeps the keypad out of the way; the button callsshow()when it is needed.- The
enterkey arrives as{ key: 'enter' }, and the script hides the keypad.
Show the last key pressed
Section titled “Show the last key pressed”Goal: echo each key in a label, for a typing tutorial.
#pragma lifecycle(startup)
// Scene: a UI Keyboard, a UI Input, and a UI Label named 'Last Key'.const NAMES = { backspace: '⌫ Backspace', space: '␣ Space', enter: '⏎ Enter' };
function startup() { const label = findEntity('Last Key').getComponent(UILabelComponent);
on('ui:keyboard', (event) => { if (event.eventType === EventType.UIKeyboard.Show) label.setProps({ text: 'Start typing' }); if (event.eventType === EventType.UIKeyboard.Press) { const key = event.data.key; label.setProps({ text: `You pressed: ${NAMES[key] ?? key}` }); } });}How it works:
- Character keys arrive as the character itself; the three special keys arrive by name and are turned into readable text.
UIKeyboard::showresets the label each time the keyboard appears.
Components
Section titled “Components”What a new UI Keyboard is made of. Required components are always present and cannot be removed; the others can. Per entity says whether an entity can hold one of that component or several. Layer, Lock and Enable are on every placed entity, and Transform holds its position, rotation and scale.
| Component | Type | Role | Per entity | What it does |
|---|---|---|---|---|
| Layer | comp-layer |
Required | One | Puts the entity on a render/raycast layer so cameras and rays can include or skip it. |
| Lock | comp-lock |
Required | One | Editor-only flag that stops the entity from being selected or moved. |
| Enable | comp-enable |
Required | One | Turns the entity and its children on or off: hides them and stops their behavior. |
| Transform | comp- |
Required | One | Position, rotation and scale of the entity, with optional editor-only per-axis locks. |
| UI Keyboard Qwerty | comp- |
Starts with | One | An on-screen letter keyboard. |
| Signal Emitter | comp- |
Starts with | Several | Collects events from sibling components and sends them on as signals. |
You can also add 1 optional components, such as UI Keyboard Numeric.
The settings of a new entity, as the runtime creates it. Change them in the Properties panel of XR Creator, or from a script with getComponent(…).setProps({ … }).
| Prop | Default | Range |
|---|---|---|
signal |
"ui:keyboard" |
Events
Section titled “Events”With a Signal Emitter on the entity, these events reach scripts as { entityId, eventType, data }. Compare eventType with the EventType constants.
UIKeyboard::pressUIKeyboard::showUIKeyboard::hide
Like every entity, it also reports: Transform::change (and position-change, rotation-change, scale-change), Enable::change, Layer::change and Lock::change.
Live example
Section titled “Live example”Run it in the browser, read its scripts, and download it as an XR Creator project.
A numeric UI Keyboard placed in the scene types into a number field; a Behavior script checks the code on submit and swings the vault door open with a tween.
Click the code field, then press 2, 4, 6, 8 and the enter key on the in-scene keypad. A wrong code clears the field. Drag outside the panel to orbit the camera, scroll to zoom.
From a script
Section titled “From a script”Find the entity by its name, check its class, and work with its components. Name the entity in the Hierarchy first; this example expects My UI Keyboard.
#pragma lifecycle(startup)
function startup() { // Find the entity by the name it has in the Hierarchy. const entity = findEntity('My UI Keyboard'); if (!(entity instanceof UIKeyboardEntity)) return;
// Its UI Keyboard Qwerty component holds the settings listed under Props. const component = entity.getComponent(UIKeyboardQWERTYComponent); log(component.props);}