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 shortcut 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.
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 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 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 the user presses a key in a TextField. This code sample highlights the event firing of both KeyUpEvent
and KeyDownEvent
.
To see the example in action, do the following:
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);
}
}
When you visit any website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.
More information
These cookies enable the website to provide enhanced functionality and personalisation. They may be set by us or by third party providers whose services we have added to our pages. If you do not allow these cookies then some or all of these services may not function properly.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. All information these cookies collect is aggregated and therefore anonymous. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
These cookies may be set through our site by our advertising partners. They may be used by those companies to build a profile of your interests and show you relevant adverts on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. If you do not allow these cookies, you will experience less targeted advertising. Some 3rd party video providers do not allow video views without targeting cookies. If you are experiencing difficulty viewing a video, you will need to set your cookie preferences for targeting to yes if you wish to view videos from these providers. Unity does not control this.
These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.