|
4 | 4 | A shortcut for binding input. |
5 | 5 | </brief_description> |
6 | 6 | <description> |
7 | | - Shortcuts are commonly used for interacting with a [Control] element from an [InputEvent] (also known as hotkeys). |
8 | | - One shortcut can contain multiple [InputEvent]s, allowing the possibility of triggering one action with multiple different inputs. |
| 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.set_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 | + public partial class YourScriptName : Godot.Node |
| 29 | + { |
| 30 | + private Godot.Shortcut saveShortcut; |
| 31 | + |
| 32 | + public override void _Ready() |
| 33 | + { |
| 34 | + // Enable input processing explicitly (optional for Node, but included for clarity) |
| 35 | + SetProcessInput(true); |
| 36 | + |
| 37 | + saveShortcut = new Godot.Shortcut(); |
| 38 | + |
| 39 | + Godot.InputEventKey keyEvent = new Godot.InputEventKey |
| 40 | + { |
| 41 | + Keycode = Godot.Key.S, |
| 42 | + CtrlPressed = true, |
| 43 | + CommandOrControlAutoremap = true |
| 44 | + }; |
| 45 | + |
| 46 | + Godot.Collections.Array<Godot.InputEvent> events = new Godot.Collections.Array<Godot.InputEvent> { keyEvent }; |
| 47 | + saveShortcut.SetEvents(events); |
| 48 | + } |
| 49 | + |
| 50 | + public override void _Input(Godot.InputEvent @event) |
| 51 | + { |
| 52 | + if (@event is Godot.InputEventKey keyEvent && |
| 53 | + saveShortcut.MatchesEvent(@event) && |
| 54 | + keyEvent.Pressed && !keyEvent.Echo) |
| 55 | + { |
| 56 | + Godot.GD.Print("Save shortcut pressed!"); |
| 57 | + GetViewport().SetInputAsHandled(); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + [/csharp] |
| 62 | + [/codeblocks] |
9 | 63 | </description> |
10 | 64 | <tutorials> |
11 | 65 | </tutorials> |
|
0 commit comments