키보드 이벤트는 키보드에서 키를 누르거나 놓을 때 발생합니다.각 이벤트에는 해당 이벤트에 대한 수정자, 텍스트 문자, 관련 키 코드에 대한 정보가 포함되어 있습니다.
많은 표준 컨트롤은 KeyDownEvent
를 사용하여 단축키 또는 접근성 동작을 인코딩합니다.다음 예에서는 모두 키보드 이벤트를 사용합니다.
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 문서의 수정 키 섹션을 참조하십시오.
다음 리스트는 이벤트 패밀리에 있는 각 이벤트의 이름, 설명, 타겟을 제공합니다.
기본적으로 시각적 요소는 키보드 이벤트를 수신하지 않습니다.포커스를 둘 수 있고 현재 포커스가 맞춰진 요소만 키보드 이벤트의 타겟이 됩니다.이는 키보드 이벤트가 하향 침투되고 버블업이 발생하여 부모 요소도 이를 수신할 수 있기 때문입니다.
요약하면, 키보드 이벤트 수신을 시작하려면 요소를 focusable=true
로 표시하고 element.Focus()
를 사용하여 명시적으로 포커스를 부여해야 합니다.이렇게 하면 요소가 키보드 이벤트를 수신할 수 있습니다.
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
: 포커스가 있는 시각적 요소입니다. 포커스가 있는 요소가 없는 경우에는 패널의 루트 시각적 요소입니다.
다음 코드 예시에서는 TextField에서 키를 누르면 콘솔에 메시지가 출력됩니다.이 코드 샘플은 KeyUpEvent
및 KeyDownEvent
의 이벤트 발동을 강조합니다.
임의의 템플릿을 사용하여 Unity 프로젝트를 생성합니다.
SampleScene에서 GameObject > UI Toolkit > UI Document를 선택합니다.
다음 콘텐츠가 포함된 KeyboardEventTest.cs
라는 이름의 C# 스크립트를 생성합니다.
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);
}
}
계층(Hierarchy) 창에서 UIDocument 게임 오브젝트를 선택합니다.
KeyboardEventTest.cs
를 인스펙터(Inspector) 창의 Add Component로 드래그합니다.
플레이 모드를 시작하고 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.