InputEventKey
继承: InputEventWithModifiers < InputEventFromWindow < InputEvent < Resource < RefCounted < Object
代表键盘上的某个按键被按下或松开。
描述
键盘上的按键操作对应的输入事件。支持按键按下、释放和 echo 事件。还可以在 Node._unhandled_key_input() 收到。
注意:从键盘上接收的事件通常设置了所有属性。事件映射应该只设置 keycode、physical_keycode、unicode 的其中之一。
比较事件时,将按以下优先级检查属性——keycode、physical_keycode、unicode。有一个匹配就会认为事件相等。
教程
属性
|
||
|
||
|
||
|
||
|
||
|
||
|
方法
as_text_key_label() const |
|
as_text_keycode() const |
|
as_text_location() const |
|
as_text_physical_keycode() const |
|
get_key_label_with_modifiers() const |
|
get_keycode_with_modifiers() const |
|
属性说明
如果为 true,则表示在该事件之前已按下该键。回显事件是用户按住该键时发送的重复按键事件。
注意:发送回显事件的速率通常约为每秒 20 个事件(按住按键约半秒钟后)。但是,在操作系统设置中,按键重复延迟/速度可被用户修改或者完全禁用。为确保你的项目在所有配置下都能正常工作,请不要假设用户在项目行为中具有特定的按键重复配置。
表示当前键盘布局中印在键上的本地化标签,对应于 Key 常量之一或任何有效的 Unicode 字符。
对于键上只有一个标签的键盘布局,它等同于 keycode。
要获得 InputEventKey 的人类可读表示,请使用 OS.get_keycode_string(event.key_label),其中 event 是 InputEventKey。
+-----+ +-----+
| Q | | Q | - "Q" - keycode
| Й | | ض | - "Й" and "ض" - key_label
+-----+ +-----+
当前键盘布局中键上打印的拉丁标签,对应于 Key 常量之一。
要获得 InputEventKey 的人类可读表示,请使用 OS.get_keycode_string(event.keycode),其中 event 是 InputEventKey。
+-----+ +-----+
| Q | | Q | - "Q" - 键码
| Й | | ض | - "Й" 和 "ض" - key_label
+-----+ +-----+
KeyLocation location = 0 🔗
void set_location(value: KeyLocation)
KeyLocation get_location()
表示具有左右版本的键的位置,例如 Shift 和 Alt。
代表按键在 101/102 键的美式键盘上的物理位置,对应一个 Key 常量。
要获取 InputEventKey 的人类可读表示,请搭配使用 OS.get_keycode_string() 和 DisplayServer.keyboard_get_keycode_from_physical():
func _input(event):
if event is InputEventKey:
var keycode = DisplayServer.keyboard_get_keycode_from_physical(event.physical_keycode)
print(OS.get_keycode_string(keycode))
public override void _Input(InputEvent @event)
{
if (@event is InputEventKey inputEventKey)
{
var keycode = DisplayServer.KeyboardGetKeycodeFromPhysical(inputEventKey.PhysicalKeycode);
GD.Print(OS.GetKeycodeString(keycode));
}
}
如果为 true,按键的状态是被按下。如果为 false,该键的状态被释放。
按键 Unicode 字符代码(当相关时),由修饰键移动。除非 IME 输入模式处于活动状态,否则复合字符和复杂文字的 Unicode 字符代码可能不可用。有关详细信息,请参阅 Window.set_ime_active()。
方法说明
String as_text_key_label() const 🔗
返回该事件 key_label 及修饰键的 String 字符串表示。
String as_text_keycode() const 🔗
返回该事件 keycode 及修饰键的 String 字符串表示。
String as_text_location() const 🔗
返回事件的 location 的 String 表示形式。如果该事件不特定于某个位置,则这将是一个空白字符串。
String as_text_physical_keycode() const 🔗
返回该事件 physical_keycode 及修饰键的 String 字符串表示。
Key get_key_label_with_modifiers() const 🔗
返回与修饰键,例如 Shift 或 Alt 组合的本地化键标签。另见 InputEventWithModifiers。
要获得带有修饰键的 InputEventKey 的人类可读表示,请使用 OS.get_keycode_string(event.get_key_label_with_modifiers()),其中 event 是 InputEventKey。
Key get_keycode_with_modifiers() const 🔗
返回与 Shift 或 Alt 等修饰键组合的拉丁键码。另见 InputEventWithModifiers。
要获得带有修饰键的 InputEventKey 的人类可读表示,请使用 OS.get_keycode_string(event.get_keycode_with_modifiers()),其中 event 是 InputEventKey。
Key get_physical_keycode_with_modifiers() const 🔗
返回与诸如 Shift 或 Alt 的修饰键组合的物理键码。另见 InputEventWithModifiers。
要获得带有修饰符的 InputEventKey 的人类可读表示,请使用 OS.get_keycode_string(event.get_physical_keycode_with_modifiers()),其中 event 是 InputEventKey。