Attention: Here be dragons

This is the latest (unstable) version of this documentation, which may document features not available in or compatible with released stable versions of Godot.

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()

If true, the key was already pressed before this event. An echo event is a repeated key event sent when the user is holding down the key.

Note: The rate at which echo events are sent is typically around 20 events per second (after holding down the key for roughly half a second). However, the key repeat delay/speed can be changed by the user or disabled entirely in the operating system settings. To ensure your project works correctly on all configurations, do not assume the user has a specific key repeat configuration in your project's behavior.


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 🔗

Represents the location of a key which has both left and right versions, such as Shift or Alt.


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 🔗

Returns a String representation of the event's location. This will be a blank string if the event is not specific to a location.


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