| 1 | <?xml version="1.0" encoding="UTF-8" ?> |
| 2 | <class name="Shortcut" inherits="Resource" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd"> |
| 3 | <brief_description> |
| 4 | A shortcut for binding input. |
| 5 | </brief_description> |
| 6 | <description> |
| 7 | Shortcuts (also known as hotkeys) are containers of [InputEvent] resources. They are commonly used to interact with a [Control] element from an [InputEvent]. |
| 8 | One shortcut can contain multiple [InputEvent] resources, making it possible to trigger one action with multiple different inputs. |
| 9 | [b]Example:[/b] Capture the [kbd]Ctrl + S[/kbd] shortcut using a [Shortcut] resource: |
| 10 | [codeblocks] |
| 11 | [gdscript] |
| 12 | extends Node |
| 13 | |
| 14 | var save_shortcut = Shortcut.new() |
| 15 | func _ready(): |
| 16 | var key_event = InputEventKey.new() |
| 17 | key_event.keycode = KEY_S |
| 18 | key_event.ctrl_pressed = true |
| 19 | key_event.command_or_control_autoremap = true # Swaps Ctrl for Command on Mac. |
| 20 | save_shortcut.events = [key_event] |
| 21 | |
| 22 | func _input(event): |
| 23 | if save_shortcut.matches_event(event) and event.is_pressed() and not event.is_echo(): |
| 24 | print("Save shortcut pressed!") |
| 25 | get_viewport().set_input_as_handled() |
| 26 | [/gdscript] |
| 27 | [csharp] |
| 28 | using Godot; |
| 29 | |
| 30 | public partial class MyNode : Node |
| 31 | { |
| 32 | private readonly Shortcut _saveShortcut = new Shortcut(); |
| 33 | |
| 34 | public override void _Ready() |
| 35 | { |
| 36 | InputEventKey keyEvent = new InputEventKey |
| 37 | { |
| 38 | Keycode = Key.S, |
| 39 | CtrlPressed = true, |
| 40 | CommandOrControlAutoremap = true, // Swaps Ctrl for Command on Mac. |
| 41 | }; |
| 42 | |
| 43 | _saveShortcut.Events = [keyEvent]; |
| 44 | } |
| 45 | |
| 46 | public override void _Input(InputEvent @event) |
| 47 | { |
| 48 | if (@event is InputEventKey keyEvent && |
| 49 | _saveShortcut.MatchesEvent(@event) && |
| 50 | keyEvent.Pressed && !keyEvent.Echo) |
| 51 | { |
| 52 | GD.Print("Save shortcut pressed!"); |
| 53 | GetViewport().SetInputAsHandled(); |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | [/csharp] |
| 58 | [/codeblocks] |
| 59 | </description> |
| 60 | <tutorials> |
| 61 | </tutorials> |
| 62 | <methods> |
| 63 | <method name="get_as_text" qualifiers="const"> |
| 64 | <return type="String" /> |
| 65 | <description> |
| 66 | Returns the shortcut's first valid [InputEvent] as a [String]. |
| 67 | </description> |
| 68 | </method> |
| 69 | <method name="has_valid_event" qualifiers="const"> |
| 70 | <return type="bool" /> |
| 71 | <description> |
| 72 | Returns whether [member events] contains an [InputEvent] which is valid. |
| 73 | </description> |
| 74 | </method> |
| 75 | <method name="matches_event" qualifiers="const"> |
| 76 | <return type="bool" /> |
| 77 | <param index="0" name="event" type="InputEvent" /> |
| 78 | <description> |
| 79 | Returns whether any [InputEvent] in [member events] equals [param event]. This uses [method InputEvent.is_match] to compare events. |
| 80 | </description> |
| 81 | </method> |
| 82 | </methods> |
| 83 | <members> |
| 84 | <member name="events" type="Array" setter="set_events" getter="get_events" default="[]"> |
| 85 | The shortcut's [InputEvent] array. |
| 86 | Generally the [InputEvent] used is an [InputEventKey], though it can be any [InputEvent], including an [InputEventAction]. |
| 87 | </member> |
| 88 | </members> |
| 89 | </class> |