class_shortcut.rst 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. :github_url: hide
  2. .. DO NOT EDIT THIS FILE!!!
  3. .. Generated automatically from Godot engine sources.
  4. .. Generator: https://github.com/godotengine/godot/tree/master/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/master/doc/classes/Shortcut.xml.
  6. .. _class_Shortcut:
  7. Shortcut
  8. ========
  9. **Inherits:** :ref:`Resource<class_Resource>` **<** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_Object>`
  10. A shortcut for binding input.
  11. .. rst-class:: classref-introduction-group
  12. Description
  13. -----------
  14. Shortcuts (also known as hotkeys) are containers of :ref:`InputEvent<class_InputEvent>` resources. They are commonly used to interact with a :ref:`Control<class_Control>` element from an :ref:`InputEvent<class_InputEvent>`.
  15. One shortcut can contain multiple :ref:`InputEvent<class_InputEvent>` resources, making it possible to trigger one action with multiple different inputs.
  16. \ **Example:** Capture the :kbd:`Ctrl + S` shortcut using a **Shortcut** resource:
  17. .. tabs::
  18. .. code-tab:: gdscript
  19. extends Node
  20. var save_shortcut = Shortcut.new()
  21. func _ready():
  22. var key_event = InputEventKey.new()
  23. key_event.keycode = KEY_S
  24. key_event.ctrl_pressed = true
  25. key_event.command_or_control_autoremap = true # Swaps ctrl for Command on Mac.
  26. save_shortcut.set_events([key_event])
  27. func _input(event):
  28. if save_shortcut.matches_event(event) and event.is_pressed() and not event.is_echo():
  29. print("Save shortcut pressed!")
  30. get_viewport().set_input_as_handled()
  31. .. code-tab:: csharp
  32. public partial class YourScriptName : Godot.Node
  33. {
  34. private Godot.Shortcut saveShortcut;
  35. public override void _Ready()
  36. {
  37. // Enable input processing explicitly (optional for Node, but included for clarity)
  38. SetProcessInput(true);
  39. saveShortcut = new Godot.Shortcut();
  40. Godot.InputEventKey keyEvent = new Godot.InputEventKey
  41. {
  42. Keycode = Godot.Key.S,
  43. CtrlPressed = true,
  44. CommandOrControlAutoremap = true
  45. };
  46. Godot.Collections.Array<Godot.InputEvent> events = new Godot.Collections.Array<Godot.InputEvent> { keyEvent };
  47. saveShortcut.SetEvents(events);
  48. }
  49. public override void _Input(Godot.InputEvent @event)
  50. {
  51. if (@event is Godot.InputEventKey keyEvent &&
  52. saveShortcut.MatchesEvent(@event) &&
  53. keyEvent.Pressed && !keyEvent.Echo)
  54. {
  55. Godot.GD.Print("Save shortcut pressed!");
  56. GetViewport().SetInputAsHandled();
  57. }
  58. }
  59. }
  60. .. rst-class:: classref-reftable-group
  61. Properties
  62. ----------
  63. .. table::
  64. :widths: auto
  65. +---------------------------+-----------------------------------------------+--------+
  66. | :ref:`Array<class_Array>` | :ref:`events<class_Shortcut_property_events>` | ``[]`` |
  67. +---------------------------+-----------------------------------------------+--------+
  68. .. rst-class:: classref-reftable-group
  69. Methods
  70. -------
  71. .. table::
  72. :widths: auto
  73. +-----------------------------+----------------------------------------------------------------------------------------------------------------------+
  74. | :ref:`String<class_String>` | :ref:`get_as_text<class_Shortcut_method_get_as_text>`\ (\ ) |const| |
  75. +-----------------------------+----------------------------------------------------------------------------------------------------------------------+
  76. | :ref:`bool<class_bool>` | :ref:`has_valid_event<class_Shortcut_method_has_valid_event>`\ (\ ) |const| |
  77. +-----------------------------+----------------------------------------------------------------------------------------------------------------------+
  78. | :ref:`bool<class_bool>` | :ref:`matches_event<class_Shortcut_method_matches_event>`\ (\ event\: :ref:`InputEvent<class_InputEvent>`\ ) |const| |
  79. +-----------------------------+----------------------------------------------------------------------------------------------------------------------+
  80. .. rst-class:: classref-section-separator
  81. ----
  82. .. rst-class:: classref-descriptions-group
  83. Property Descriptions
  84. ---------------------
  85. .. _class_Shortcut_property_events:
  86. .. rst-class:: classref-property
  87. :ref:`Array<class_Array>` **events** = ``[]`` :ref:`🔗<class_Shortcut_property_events>`
  88. .. rst-class:: classref-property-setget
  89. - |void| **set_events**\ (\ value\: :ref:`Array<class_Array>`\ )
  90. - :ref:`Array<class_Array>` **get_events**\ (\ )
  91. The shortcut's :ref:`InputEvent<class_InputEvent>` array.
  92. Generally the :ref:`InputEvent<class_InputEvent>` used is an :ref:`InputEventKey<class_InputEventKey>`, though it can be any :ref:`InputEvent<class_InputEvent>`, including an :ref:`InputEventAction<class_InputEventAction>`.
  93. .. rst-class:: classref-section-separator
  94. ----
  95. .. rst-class:: classref-descriptions-group
  96. Method Descriptions
  97. -------------------
  98. .. _class_Shortcut_method_get_as_text:
  99. .. rst-class:: classref-method
  100. :ref:`String<class_String>` **get_as_text**\ (\ ) |const| :ref:`🔗<class_Shortcut_method_get_as_text>`
  101. Returns the shortcut's first valid :ref:`InputEvent<class_InputEvent>` as a :ref:`String<class_String>`.
  102. .. rst-class:: classref-item-separator
  103. ----
  104. .. _class_Shortcut_method_has_valid_event:
  105. .. rst-class:: classref-method
  106. :ref:`bool<class_bool>` **has_valid_event**\ (\ ) |const| :ref:`🔗<class_Shortcut_method_has_valid_event>`
  107. Returns whether :ref:`events<class_Shortcut_property_events>` contains an :ref:`InputEvent<class_InputEvent>` which is valid.
  108. .. rst-class:: classref-item-separator
  109. ----
  110. .. _class_Shortcut_method_matches_event:
  111. .. rst-class:: classref-method
  112. :ref:`bool<class_bool>` **matches_event**\ (\ event\: :ref:`InputEvent<class_InputEvent>`\ ) |const| :ref:`🔗<class_Shortcut_method_matches_event>`
  113. Returns whether any :ref:`InputEvent<class_InputEvent>` in :ref:`events<class_Shortcut_property_events>` equals ``event``. This uses :ref:`InputEvent.is_match()<class_InputEvent_method_is_match>` to compare events.
  114. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  115. .. |required| replace:: :abbr:`required (This method is required to be overridden when extending its base class.)`
  116. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  117. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  118. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  119. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  120. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`
  121. .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)`
  122. .. |void| replace:: :abbr:`void (No return value.)`