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.
Checking the stable version of the documentation...
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 |
|
屬性說明
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.
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
+-----+ +-----+
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 🔗
void set_location(value: KeyLocation)
KeyLocation get_location()
Represents the location of a key which has both left and right versions, such as Shift or Alt.
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))
public override void _Input(InputEvent @event)
{
if (@event is InputEventKey inputEventKey)
{
var keycode = DisplayServer.KeyboardGetKeycodeFromPhysical(inputEventKey.PhysicalKeycode);
var label = DisplayServer.KeyboardGetLabelFromPhysical(inputEventKey.PhysicalKeycode);
GD.Print(OS.GetKeycodeString(keycode));
GD.Print(OS.GetKeycodeString(label));
}
}
如果為 true,按鍵的狀態是被按下。如果為 false,該鍵的狀態被釋放。
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 🔗
返回與修飾鍵,例如 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。