Version: 2023.1

Event.KeyboardEvent

切换到手册
public static Event KeyboardEvent (string key);

参数

key A string representing keyboard keys and modifiers.

返回

Event A new Event with EventType.KeyDown and the requested KeyCode and optional EventModifier.

描述

创建键盘事件。

当您需要检查某个键是否已被按下时,这很有用 - 可能具有修改键。键字符串的语法是键名称 (与 Input Manager 中的相同),可选择将任意数量的修改键作为前缀:\ & = Alternate,^ = Control,% = Command/Windows 键,# = Shift \ 示例:&f12 = Alternate + F12,"^[0]" = Control + keypad0。\

See the Input Manager manual page for more information on key names.

using UnityEngine;

public class Example : MonoBehaviour { // Detects if the Enter key was pressed void OnGUI() { GUILayout.Label("Press Enter To Start Game");

if (Event.current.Equals(Event.KeyboardEvent("[enter]"))) { Application.LoadLevel(1); }

if (Event.current.Equals(Event.KeyboardEvent("return"))) { Debug.Log("I said enter, not return - try the keypad"); } } }