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

KeyLocation

location

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_location() 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(value: bool)

  • bool is_echo()

如果为 true,则表示在该事件之前已按下该键。回显事件是用户按住该键时发送的重复按键事件。

注意:发送回显事件的速率通常约为每秒 20 个事件(按住按键约半秒钟后)。但是,在操作系统设置中,按键重复延迟/速度可被用户修改或者完全禁用。为确保你的项目在所有配置下都能正常工作,请不要假设用户在项目行为中具有特定的按键重复配置。


Key key_label = 0 🔗

  • void set_key_label(value: Key)

  • Key get_key_label()

Represents the localized label printed on the key in the current keyboard layout, which corresponds to one of the Key constants or any valid Unicode character. Key labels are meant for key prompts.

For keyboard layouts with a single label on the key, it is equivalent to keycode.

To get a human-readable representation of the InputEventKey, use OS.get_keycode_string(event.key_label) where event is the InputEventKey.

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

Key keycode = 0 🔗

  • void set_keycode(value: Key)

  • Key get_keycode()

Latin label printed on the key in the current keyboard layout, which corresponds to one of the Key constants. Key codes are meant for shortcuts expressed with a standard Latin keyboard, such as Ctrl + S for a "Save" shortcut.

To get a human-readable representation of the InputEventKey, use OS.get_keycode_string(event.keycode) where event is the InputEventKey.

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

KeyLocation location = 0 🔗

表示具有左右版本的键的位置,例如 ShiftAlt


Key physical_keycode = 0 🔗

  • void set_physical_keycode(value: Key)

  • Key get_physical_keycode()

Represents the physical location of a key on the 101/102-key US QWERTY keyboard, which corresponds to one of the Key constants. Physical key codes meant for game input, such as WASD movement, where only the location of the keys is important.

To get a human-readable representation of the InputEventKey, use OS.get_keycode_string() in combination with DisplayServer.keyboard_get_keycode_from_physical() or DisplayServer.keyboard_get_label_from_physical():

func _input(event):
    if event is InputEventKey:
        var keycode = DisplayServer.keyboard_get_keycode_from_physical(event.physical_keycode)
        var label = DisplayServer.keyboard_get_label_from_physical(event.physical_keycode)
        print(OS.get_keycode_string(keycode))
        print(OS.get_keycode_string(label))

bool pressed = false 🔗

  • void set_pressed(value: bool)

  • bool is_pressed()

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


int unicode = 0 🔗

  • void set_unicode(value: int)

  • int get_unicode()

The key Unicode character code (when relevant), shifted by modifier keys. Unicode character codes for composite characters and complex scripts may not be available unless IME input mode is active. See Window.set_ime_active() for more information. Unicode character codes are meant for text input.

Note: This property is set by the engine only for a pressed event. If the event is sent by an IME or a virtual keyboard, no corresponding key released event is sent.


方法说明

String as_text_key_label() const 🔗

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


String as_text_keycode() const 🔗

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


String as_text_location() const 🔗

返回事件的 locationString 表示形式。如果该事件不特定于某个位置,则这将是一个空白字符串。


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