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
및 Button
클래스는 마우스 클릭에 대한 대체 동작으로 Enter
및 Spacebar
키 누름을 수신합니다.keyCode
프로퍼티와 문자 프로퍼티를 모두 확인하여 특수 동작을 실행하거나 텍스트를 수락합니다.모든 키보드 이벤트의 기본 클래스는 KeyboardEventBase입니다.
이벤트 | 설명 | 트리클다운 | 버블업 | 취소 가능 |
---|---|---|---|---|
KeyDownEvent | 사용자가 키보드 키를 누를 때 전송됩니다. | 지원 | 지원 | 지원 |
KeyUpEvent | 사용자가 키보드 키를 놓을 때 전송됩니다. | 지원 | 지원 | 지원 |
keyCode
: keyCode
프로퍼티는 키보드나 조이스틱 같은 입력 기기의 물리적 키에 직접 대응하는 문자 키를 반환합니다. character
프로퍼티와 keyCode
프로퍼티의 차이점은 keyCode
는 물리적 키를 나타내고 character
는 특정 문자의 입력을 나타낸다는 것입니다. 예를 들어 a
와 A
는 모두 keyDownEvent
동안 keyCode=KeyCode.A
를 반환합니다.
character
: character
프로퍼티는 keyDownEvent
동안 문자 코드를 반환합니다.
modifiers
: modifiers
프로퍼티는 누르고 있는 수정 키를 반환합니다. 수정 키의 몇 가지 예로는 Shift
, Ctrl
또는 Alt
키가 있습니다.
자세한 내용은 MDN 문서의 수정 키 섹션을 참조하십시오.
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 focusable=true
and explicitly give it focus using element.Focus()
. This ensures that the element is eligible to receive keyboard events.
KeyDownEvent는 키보드 키를 누를 때마다 전송됩니다. 누른 키에는 해당 이벤트에 대한 keyCode
프로퍼티가 포함되어 있습니다. 해당 키 누름에 연결된 텍스트 입력이 있는 경우 텍스트 입력의 각 문자에 대해 추가 이벤트가 전송됩니다. character
프로퍼티에는 해당 이벤트에 대한 문자가 포함됩니다.
a
를 누르거나 놓으면 UI 툴킷이 다음 이벤트를 전송합니다.
KeyDownEvent { keyCode=KeyCode.A }
KeyDownEvent { character=’a’ }
KeyUpEvent { keyCode=KeyCode.A }
Ctrl+a
를 누르거나 놓으면 UI 툴킷이 다음 이벤트를 전송합니다.
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
: 포커스가 있는 시각적 요소입니다. 포커스가 있는 요소가 없는 경우에는 패널의 루트 시각적 요소입니다.
KeyUpEvent는 키보드 키를 놓을 때 전송됩니다. 해당 이벤트에 대한 keyCode 프로퍼티에는 놓는 키가 포함되어 있습니다. KeyDownEvent
에는 키 입력에 연결된 텍스트 입력이 있을 때 전송되는 추가 이벤트가 있습니다.
a
를 누르거나 놓으면 UI 툴킷이 다음 이벤트를 전송합니다.
KeyDownEvent { keyCode=KeyCode.A }
KeyDownEvent { character=’a’ }
KeyUpEvent { keyCode=KeyCode.A }
Ctrl+a
를 누르거나 놓으면 UI 툴킷이 다음 이벤트를 전송합니다.
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 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 GameObject in the Hierarchy window.
Drag KeyboardEventTest.cs
to Add Component in the Inspector window.
Enter Play mode and type in the TextField.