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 KeyDownEvent
to encode shortcuts or accessibility behaviors. The following examples all use keyboard events:
Toggle
and Button
classes listen for Enter
and Spacebar
key presses as replacement actions for mouse clicks.keyCode
property and the character property to execute special actions or to accept text.The base class for all keyboard events is KeyboardEventBase.
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 |
keyCode
: The keyCode
property returns a character key that corresponds directly to a physical key on an input device, such as a keyboard or joystick. The difference between the character
property and the keyCode
property is that keyCode
represents a physical key, while character
represents the entry of a specific character. For example, both a
and A
return keyCode=KeyCode.A
during a keyDownEvent
.
character
: The character
property returns a character code during a keyDownEvent
.
modifiers
: The modifiers
property returns which modifier key is held down. Some examples of modifier keys are the Shift
, Ctrl
, or Alt
keys.
For more information, see the Modifier keys section of the MDN documentation.
The following list provides the name, description, and target of each event in the event family.
By default, a visual elementA node of a visual tree that instantiates or derives from the C# VisualElement
class. You can style the look, define the behaviour, and display it on screen as part of the UI. More info
See in Glossary 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 focusable=true
and explicitly give it focus using element.Focus()
. This ensures that the element is eligible to receive keyboard events.
A KeyDownEvent is sent each time you press a key on the keyboard. The key pressed contains the keyCode
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 character
property contains the character for those events.
When you press and release a
, UI(User Interface) Allows a user to interact with your application. Unity currently supports three UI systems. More info
See in Glossary Toolkit sends the following events:
KeyDownEvent { keyCode=KeyCode.A }
KeyDownEvent { character=’a’ }
KeyUpEvent { keyCode=KeyCode.A }
When you press and release Ctrl+a
, UI Toolkit sends the following events:
KeyDownEvent { keyCode=KeyCode.LeftControl, modifiers=EventModifiers.Control }
KeyDownEvent { keyCode=KeyCode.A, modifiers=EventModifiers.Control }
KeyUpEvent { keyCode=KeyCode.A, modifiers=EventModifiers.Control }
KeyUpEvent { keyCode=KeyCode.LeftControl }
target
: The visual element that has focus. If no element has focus, the root visual element of the panel.
A KeyUpEvent is sent when you release a key on the keyboard. The keyCode property for that event contains the key being released. KeyDownEvent
has additional events sent when a keystroke has an associated text input.
When you press and release a
, UI Toolkit sends the following events:
KeyDownEvent { keyCode=KeyCode.A }
KeyDownEvent { character=’a’ }
KeyUpEvent { keyCode=KeyCode.A }
When you press and release Ctrl+a
, UI Toolkit sends the following events:
KeyDownEvent { keyCode=KeyCode.LeftControl, modifiers=EventModifiers.Control }
KeyDownEvent { keyCode=KeyCode.A, modifiers=EventModifiers.Control }
KeyUpEvent { keyCode=KeyCode.A, modifiers=EventModifiers.Control }
KeyUpEvent { keyCode=KeyCode.LeftControl }
target
: The visual element that has focus. If no element has focus, the root visual element of the panel.
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 KeyUpEvent
and KeyDownEvent
.
Create a Unity project with any template.
In the SampleScene, select GameObject > UI Toolkit > UI Document.
Create a C# script named KeyboardEventTest.cs
with the following contents:
using 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 GameObjectThe fundamental object in Unity scenes, which can represent characters, props, scenery, cameras, waypoints, and more. A GameObject’s functionality is defined by the Components attached to it. More info
See in Glossary in the Hierarchy window.
Drag KeyboardEventTest.cs
to Add Component in the InspectorA Unity window that displays information about the currently selected GameObject, asset or project settings, allowing you to inspect and edit the values. More info
See in Glossary window.
Enter Play mode and type in the TextField.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.