Up to date

This page is up to date for Godot 4.2. If you still find outdated information, please open an issue.

InputEventKey

继承: InputEventWithModifiers < InputEventFromWindow < InputEvent < Resource < RefCounted < Object

代表键盘上的某个按键被按下或松开。

描述

键盘上的按键操作对应的输入事件。支持按键按下、释放和 echo 事件。还可以在 Node._unhandled_key_input 收到。

注意:从键盘上接收的事件通常设置了所有属性。事件映射应该只设置 keycodephysical_keycodeunicode 的其中之一。

比较事件时,将按以下优先级检查属性——keycodephysical_keycodeunicode。有一个匹配就会认为事件相等。

教程

属性

bool

echo

false

Key

key_label

0

Key

keycode

0

Key

physical_keycode

0

bool

pressed

false

int

unicode

0

方法

String

as_text_key_label ( ) const

String

as_text_keycode ( ) const

String

as_text_physical_keycode ( ) const

Key

get_key_label_with_modifiers ( ) const

Key

get_keycode_with_modifiers ( ) const

Key

get_physical_keycode_with_modifiers ( ) const


属性说明

bool echo = false

  • void set_echo ( bool value )

  • bool is_echo ( )

如果为 true,则该键在此事件之前已被按下。这意味着用户正在按住该键。


Key key_label = 0

  • void set_key_label ( Key value )

  • Key get_key_label ( )

表示当前键盘布局中印在键上的本地化标签,对应于 Key 常量之一或任何有效的 Unicode 字符。

对于键上只有一个标签的键盘布局,它等同于 keycode

要获得 InputEventKey 的人类可读表示,请使用 OS.get_keycode_string(event.key_label),其中 eventInputEventKey

+-----+ +-----+
| Q   | | Q   | - "Q" - keycode
|   Й | |  ض | - "Й" and "ض" - key_label
+-----+ +-----+

Key keycode = 0

  • void set_keycode ( Key value )

  • Key get_keycode ( )

当前键盘布局中键上打印的拉丁标签,对应于 Key 常量之一。

要获得 InputEventKey 的人类可读表示,请使用 OS.get_keycode_string(event.keycode),其中 eventInputEventKey

+-----+ +-----+
| Q   | | Q   | - "Q" - 键码
|   Й | |  ض | - "Й"  "ض" - key_label
+-----+ +-----+

Key physical_keycode = 0

  • void set_physical_keycode ( Key value )

  • Key get_physical_keycode ( )

代表按键在 101/102 键的美式键盘上的物理位置,对应一个 Key 常量。

要获取 InputEventKey 的人类可读表示,请搭配使用 OS.get_keycode_stringDisplayServer.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))

bool pressed = false

  • void set_pressed ( bool value )

  • bool is_pressed ( )

如果为 true,按键的状态是被按下。如果为 false,该键的状态被释放。


int unicode = 0

  • void set_unicode ( int value )

  • int get_unicode ( )

按键 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_physical_keycode ( ) const

返回该事件 physical_keycode 及修饰键的 String 字符串表示。


Key get_key_label_with_modifiers ( ) const

返回与修饰键,例如 ShiftAlt 组合的本地化键标签。另见 InputEventWithModifiers

要获得带有修饰键的 InputEventKey 的人类可读表示,请使用 OS.get_keycode_string(event.get_key_label_with_modifiers()),其中 eventInputEventKey


Key get_keycode_with_modifiers ( ) const

返回与 ShiftAlt 等修饰键组合的拉丁键码。另见 InputEventWithModifiers

要获得带有修饰键的 InputEventKey 的人类可读表示,请使用 OS.get_keycode_string(event.get_keycode_with_modifiers()),其中 eventInputEventKey


Key get_physical_keycode_with_modifiers ( ) const

返回与诸如 ShiftAlt 的修饰键组合的物理键码。另请参阅 InputEventWithModifiers

要获得带有修饰符的 InputEventKey 的人类可读表示,请使用 OS.get_keycode_string(event.get_physical_keycode_with_modifiers()),其中 eventInputEventKey