Keyboard events
Events that occur when the user presses a key.
Read time 4 minutesLast updated a month ago
Keyboard events occur when you press or release keys on the keyboard. Each event includes information about the modifier, text character, and related key code for the event.
Many standard controls use the to encode shortcuts or accessibility behaviors. The following examples all use keyboard events:
KeyDownEvent- The and
Toggleclasses listen forButtonandEnterkey presses as replacement actions for mouse clicks.Spacebar - The ScrollView and Slider controls use directional arrow key presses to modulate their values.
- The TextField control looks at both the property and the character property to execute special actions or to accept text.
keyCode
The base class for all keyboard events is KeyboardEventBase.
The following table describes the keyboard events and their propagation phases:
Event | Description | Trickles down | Bubbles up | Cancellable |
|---|---|---|---|---|
| KeyDownEvent | Sent when the user presses a key on the keyboard. | Yes | Yes | Yes |
| KeyUpEvent | Sent when the user releases a key on the keyboard. | Yes | Yes | Yes |
Unique properties
keyCodekeyCodecharacterkeyCodekeyCodecharacteraAkeyCode=KeyCode.AkeyDownEventcharactercharacterkeyDownEventmodifiersmodifiersShiftCtrlAltFor more information, see the Modifier keys section of the MDN documentation.
Event List
The following list provides the name, description, and target of each event in the event family.
By default, a visual element doesn't receive keyboard events. Only elements that are focusable and currently in focus are targeted for keyboard events. This is because keyboard events trickle down and bubble up, allowing parent elements to receive them as well.
In summary, to begin receiving keyboard events, you must mark the element as and explicitly give it focus using . This ensures that the element is eligible to receive keyboard events.
focusable=trueelement.Focus()KeyDownEvent
A KeyDownEvent is sent each time you press a key on the keyboard. The key pressed contains the property for that event. If that key press has text input associated with it, additional events are sent for each character of text input. The property contains the character for those events.
keyCodecharacterWhen you press and release , UI Toolkit sends the following events:
aKeyDownEvent { keyCode=KeyCode.A }KeyDownEvent { character=’a’ }KeyUpEvent { keyCode=KeyCode.A }
When you press and release , UI Toolkit sends the following events:
Ctrl+aKeyDownEvent { keyCode=KeyCode.LeftControl, modifiers=EventModifiers.Control }KeyDownEvent { keyCode=KeyCode.A, modifiers=EventModifiers.Control }KeyUpEvent { keyCode=KeyCode.A, modifiers=EventModifiers.Control }KeyUpEvent { keyCode=KeyCode.LeftControl }
targetKeyUpEvent
A KeyUpEvent is sent when you release a key on the keyboard. The keyCode property for that event contains the key being released. has additional events sent when a keystroke has an associated text input.
KeyDownEventWhen you press and release , UI Toolkit sends the following events:
aKeyDownEvent { keyCode=KeyCode.A }KeyDownEvent { character=’a’ }KeyUpEvent { keyCode=KeyCode.A }
When you press and release , UI Toolkit sends the following events:
Ctrl+aKeyDownEvent { keyCode=KeyCode.LeftControl, modifiers=EventModifiers.Control }KeyDownEvent { keyCode=KeyCode.A, modifiers=EventModifiers.Control }KeyUpEvent { keyCode=KeyCode.A, modifiers=EventModifiers.Control }KeyUpEvent { keyCode=KeyCode.LeftControl }
targetExamples
The following code example prints a message to the console when you press a key in a TextField. This code sample highlights the event firing of both and .
KeyUpEventKeyDownEvent-
Create a Unity project with any template.
-
In the SampleScene, select> UI Toolkit > UI Document.GameObject
?
-
Create a C# script namedwith the following contents:
KeyboardEventTest.csusing UnityEngine;using UnityEngine.UIElements;// Add KeyboardEventTest to a GameObject with a valid UIDocument.// When the user presses a key, it will print the keyboard event properties to the console.[RequireComponent(typeof(UIDocument))]public class KeyboardEventTest : MonoBehaviour{ void OnEnable() { var root = GetComponent<UIDocument>().rootVisualElement; root.Add(new Label("Press any key to see the keyDown properties")); root.Add(new TextField()); root.Q<TextField>().Focus(); root.RegisterCallback<KeyDownEvent>(OnKeyDown, TrickleDown.TrickleDown); root.RegisterCallback<KeyUpEvent>(OnKeyUp, TrickleDown.TrickleDown); } void OnKeyDown(KeyDownEvent ev) { Debug.Log("KeyDown:" + ev.keyCode); Debug.Log("KeyDown:" + ev.character); Debug.Log("KeyDown:" + ev.modifiers); } void OnKeyUp(KeyUpEvent ev) { Debug.Log("KeyUp:" + ev.keyCode); Debug.Log("KeyUp:" + ev.character); Debug.Log("KeyUp:" + ev.modifiers); }} -
Select the UIDocument GameObject in the Hierarchy window.
-
Dragto Add Component in the Inspector window.
KeyboardEventTest.cs -
Enter Play mode and type in the TextField.