:github_url: hide .. _class_Shortcut: Shortcut ======== **繼承:** :ref:`Resource` **<** :ref:`RefCounted` **<** :ref:`Object` 用於綁定輸入的快捷鍵。 .. rst-class:: classref-introduction-group 說明 ---- Shortcuts (also known as hotkeys) are containers of :ref:`InputEvent` resources. They are commonly used to interact with a :ref:`Control` element from an :ref:`InputEvent`. One shortcut can contain multiple :ref:`InputEvent` resources, making it possible to trigger one action with multiple different inputs. \ **Example:** Capture the :kbd:`Ctrl + S` shortcut using a **Shortcut** resource: .. tabs:: .. code-tab:: gdscript extends Node var save_shortcut = Shortcut.new() func _ready(): var key_event = InputEventKey.new() key_event.keycode = KEY_S key_event.ctrl_pressed = true key_event.command_or_control_autoremap = true # Swaps Ctrl for Command on Mac. save_shortcut.events = [key_event] func _input(event): if save_shortcut.matches_event(event) and event.is_pressed() and not event.is_echo(): print("Save shortcut pressed!") get_viewport().set_input_as_handled() .. code-tab:: csharp using Godot; public partial class MyNode : Node { private readonly Shortcut _saveShortcut = new Shortcut(); public override void _Ready() { InputEventKey keyEvent = new InputEventKey { Keycode = Key.S, CtrlPressed = true, CommandOrControlAutoremap = true, // Swaps Ctrl for Command on Mac. }; _saveShortcut.Events = [keyEvent]; } public override void _Input(InputEvent @event) { if (@event is InputEventKey keyEvent && _saveShortcut.MatchesEvent(@event) && keyEvent.Pressed && !keyEvent.Echo) { GD.Print("Save shortcut pressed!"); GetViewport().SetInputAsHandled(); } } } .. rst-class:: classref-reftable-group 屬性 ---- .. table:: :widths: auto +---------------------------+-----------------------------------------------+--------+ | :ref:`Array` | :ref:`events` | ``[]`` | +---------------------------+-----------------------------------------------+--------+ .. rst-class:: classref-reftable-group 方法 ---- .. table:: :widths: auto +-----------------------------+----------------------------------------------------------------------------------------------------------------------+ | :ref:`String` | :ref:`get_as_text`\ (\ ) |const| | +-----------------------------+----------------------------------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`has_valid_event`\ (\ ) |const| | +-----------------------------+----------------------------------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`matches_event`\ (\ event\: :ref:`InputEvent`\ ) |const| | +-----------------------------+----------------------------------------------------------------------------------------------------------------------+ .. rst-class:: classref-section-separator ---- .. rst-class:: classref-descriptions-group 屬性說明 -------- .. _class_Shortcut_property_events: .. rst-class:: classref-property :ref:`Array` **events** = ``[]`` :ref:`🔗` .. rst-class:: classref-property-setget - |void| **set_events**\ (\ value\: :ref:`Array`\ ) - :ref:`Array` **get_events**\ (\ ) 快捷鍵的 :ref:`InputEvent` 陣列。 通常使用的 :ref:`InputEvent` 是 :ref:`InputEventKey`\ ,儘管也可以是任何 :ref:`InputEvent`\ ,包括 :ref:`InputEventAction`\ 。 .. rst-class:: classref-section-separator ---- .. rst-class:: classref-descriptions-group 方法說明 -------- .. _class_Shortcut_method_get_as_text: .. rst-class:: classref-method :ref:`String` **get_as_text**\ (\ ) |const| :ref:`🔗` 返回該快捷鍵的第一個有效 :ref:`InputEvent` 的 :ref:`String` 形式。 .. rst-class:: classref-item-separator ---- .. _class_Shortcut_method_has_valid_event: .. rst-class:: classref-method :ref:`bool` **has_valid_event**\ (\ ) |const| :ref:`🔗` 返回 :ref:`events` 是否包含有效的 :ref:`InputEvent`\ 。 .. rst-class:: classref-item-separator ---- .. _class_Shortcut_method_matches_event: .. rst-class:: classref-method :ref:`bool` **matches_event**\ (\ event\: :ref:`InputEvent`\ ) |const| :ref:`🔗` Returns whether any :ref:`InputEvent` in :ref:`events` equals ``event``. This uses :ref:`InputEvent.is_match()` to compare events. .. |virtual| replace:: :abbr:`virtual (本方法通常需要使用者覆寫才能生效。)` .. |required| replace:: :abbr:`required (This method is required to be overridden when extending its base class.)` .. |const| replace:: :abbr:`const (本方法沒有副作用。不會修改該實例的任何成員變數。)` .. |vararg| replace:: :abbr:`vararg (本方法除了這裡描述的參數外,還可以接受任意數量的參數。)` .. |constructor| replace:: :abbr:`constructor (本方法用於建構一個型別。)` .. |static| replace:: :abbr:`static (本方法無需實例即可呼叫,因此可以直接使用類別名稱呼叫。)` .. |operator| replace:: :abbr:`operator (本方法描述將本型別作為左運算元時可用的有效運算子。)` .. |bitfield| replace:: :abbr:`BitField (此值是由下列旗標組成的位元遮罩整數。)` .. |void| replace:: :abbr:`void (無回傳值。)`