KeyBindingField
KeyBindingField in UIToolbox is a field for a key or button binding in a settings screen. Its value is an Input System control path such as <Keyboard>/space or <Gamepad>/buttonSouth. A click, Enter or the gamepad submit button starts the capture. The next key or button becomes the value. The capture is the Input System interactive rebind on a temporary action, so the paths are generic: a press on any gamepad gives <Gamepad>/.... The field needs the Input System package and an active input handler that includes it.
<tb:KeyBindingField name="jump" label="Jump" value="<Keyboard>/space" />
<tb:KeyBindingField name="jumpPad" label="Jump (pad)" devices="Gamepad" />
// Apply the value to an action. An empty value turns the binding off.
jumpField.RegisterValueChangedCallback(evt => jumpAction.ApplyBindingOverride(0, evt.newValue));
| Member | Description |
|---|---|
value |
The control path, or an empty string for no binding. |
Devices |
KeyBindingDevices flags: Keyboard, Mouse, Gamepad, All. Default keyboard and gamepad. |
EmptyText, PromptText |
The text for an empty value ("None") and during the capture ("Press a key"). |
StartListening(), CancelListening(), IsListening |
The capture. A cancel keeps the value. |
DisplayText |
The text that the field shows, for example "Space", "A" or "LMB". |
ConflictGroup (conflict-group) |
A group name. The fields of the group must have different values. Empty (default) makes no check. |
HasConflict |
True while a different field of the group has the value of this field. |
- Escape, a click elsewhere or the loss of focus cancels the capture. With
MouseinDevices, a click elsewhere binds that mouse button instead. - Delete and Backspace clear the value.
- During the capture, and for the rest of the frame after it, the field keeps the UI events of the pressed key. Tab, the arrows or Escape do not also move the focus or close a window.
- A click on the field during the capture does not start a new capture. With
MouseinDevices, the button of that click becomes the value. Without it, the click does nothing and the capture goes on.
Conflicts
Some games let two actions have the same key, so the check is optional. Give the fields that must be different the same conflict-group:
<tb:KeyBindingField label="Jump" value="<Keyboard>/space" conflict-group="keys" />
<tb:KeyBindingField label="Crouch" value="<Keyboard>/c" conflict-group="keys" />
- When two fields of the group have the same value, each gets the error look of FieldError: a border and a label in
--tb-color-danger. - The field that changed last also shows an error callout, for example "Also assigned to Jump". The text is the
tb.key-conflicttext ofToolboxText. - The field keeps the value. The error goes away when the values are different again. To refuse a value, set the old value again in your value callback when
HasConflictis true. - Empty values are never a conflict.
- The check is for the fields in one panel. For bindings in other screens, compare the values in your code and use
FieldError.Show.
Styling
| Class | Element |
|---|---|
tb-key-binding-field |
The field. It is a BaseField, so the native label and input classes apply too. |
unity-base-field__input |
The key cap: --tb-color-surface-alt with a --tb-color-border border, 2 px at the bottom. --tb-color-surface-hover on hover, and an --tb-color-accent border with the focus. |
tb-key-binding-field__key |
The text of the binding, bold --tb-color-text. |
tb-key-binding-field--empty |
No binding: normal --tb-color-text-muted text. |
tb-key-binding-field--listening |
During the capture: the cap is --tb-color-accent with italic --tb-color-accent-text text. |