Skip to content

Commit 77f2623

Browse files
committed
Merge pull request godotengine#107252 from GlitchedCode922/shortcuts
Update `Shortcut` class reference
2 parents 3799846 + 3a30a1c commit 77f2623

File tree

1 file changed

+56
-2
lines changed

1 file changed

+56
-2
lines changed

doc/classes/Shortcut.xml

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,62 @@
44
A shortcut for binding input.
55
</brief_description>
66
<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&lt;Godot.InputEvent&gt; events = new Godot.Collections.Array&lt;Godot.InputEvent&gt; { keyEvent };
47+
saveShortcut.SetEvents(events);
48+
}
49+
50+
public override void _Input(Godot.InputEvent @event)
51+
{
52+
if (@event is Godot.InputEventKey keyEvent &amp;&amp;
53+
saveShortcut.MatchesEvent(@event) &amp;&amp;
54+
keyEvent.Pressed &amp;&amp; !keyEvent.Echo)
55+
{
56+
Godot.GD.Print("Save shortcut pressed!");
57+
GetViewport().SetInputAsHandled();
58+
}
59+
}
60+
}
61+
[/csharp]
62+
[/codeblocks]
963
</description>
1064
<tutorials>
1165
</tutorials>

0 commit comments

Comments
 (0)