2017-09-12 17:42:36 -03:00
<?xml version="1.0" encoding="UTF-8" ?>
2023-07-06 10:08:05 +02:00
<class name= "Shortcut" inherits= "Resource" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation= "../class.xsd" >
2017-09-12 17:42:36 -03:00
<brief_description >
2017-11-26 14:58:38 -05:00
A shortcut for binding input.
2017-09-12 17:42:36 -03:00
</brief_description>
<description >
2025-06-07 08:32:16 +00:00
Shortcuts (also known as hotkeys) are containers of [InputEvent] resources. They are commonly used to interact with a [Control] element from an [InputEvent].
One shortcut can contain multiple [InputEvent] resources, making it possible to trigger one action with multiple different inputs.
[b]Example:[/b] Capture the [kbd]Ctrl + S[/kbd] shortcut using a [Shortcut] resource:
[codeblocks]
[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
2025-06-16 21:36:11 +02:00
key_event.command_or_control_autoremap = true # Swaps Ctrl for Command on Mac.
save_shortcut.events = [key_event]
2025-06-07 08:32:16 +00:00
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()
[/gdscript]
[csharp]
2025-06-16 21:36:11 +02:00
using Godot;
2025-06-07 08:32:16 +00:00
2025-06-16 21:36:11 +02:00
public partial class MyNode : Node
{
private readonly Shortcut _saveShortcut = new Shortcut();
2025-06-07 08:32:16 +00:00
2025-06-16 21:36:11 +02:00
public override void _Ready()
{
InputEventKey keyEvent = new InputEventKey
{
Keycode = Key.S,
CtrlPressed = true,
CommandOrControlAutoremap = true, // Swaps Ctrl for Command on Mac.
};
2025-06-07 08:32:16 +00:00
2025-06-16 21:36:11 +02:00
_saveShortcut.Events = [keyEvent];
}
2025-06-07 08:32:16 +00:00
2025-06-16 21:36:11 +02:00
public override void _Input(InputEvent @event)
{
if (@event is InputEventKey keyEvent & &
_saveShortcut.MatchesEvent(@event) & &
keyEvent.Pressed & & !keyEvent.Echo)
2025-06-07 08:32:16 +00:00
{
2025-06-16 21:36:11 +02:00
GD.Print("Save shortcut pressed!");
GetViewport().SetInputAsHandled();
2025-06-07 08:32:16 +00:00
}
}
2025-06-16 21:36:11 +02:00
}
2025-06-07 08:32:16 +00:00
[/csharp]
[/codeblocks]
2017-09-12 17:42:36 -03:00
</description>
<tutorials >
</tutorials>
<methods >
<method name= "get_as_text" qualifiers= "const" >
2021-07-30 15:28:05 +02:00
<return type= "String" />
2017-09-12 17:42:36 -03:00
<description >
2021-06-21 11:34:50 +10:00
Returns the shortcut's first valid [InputEvent] as a [String].
2017-09-12 17:42:36 -03:00
</description>
</method>
2021-08-03 17:27:45 +02:00
<method name= "has_valid_event" qualifiers= "const" >
2021-07-30 15:28:05 +02:00
<return type= "bool" />
2017-09-12 17:42:36 -03:00
<description >
2021-06-21 11:34:50 +10:00
Returns whether [member events] contains an [InputEvent] which is valid.
2017-09-12 17:42:36 -03:00
</description>
</method>
2021-08-03 17:27:45 +02:00
<method name= "matches_event" qualifiers= "const" >
2021-07-30 15:28:05 +02:00
<return type= "bool" />
2022-08-06 21:11:48 +03:00
<param index= "0" name= "event" type= "InputEvent" />
2017-09-12 17:42:36 -03:00
<description >
2024-09-17 03:20:36 -07:00
Returns whether any [InputEvent] in [member events] equals [param event]. This uses [method InputEvent.is_match] to compare events.
2017-09-12 17:42:36 -03:00
</description>
</method>
</methods>
<members >
2021-06-21 11:34:50 +10:00
<member name= "events" type= "Array" setter= "set_events" getter= "get_events" default= "[]" >
The shortcut's [InputEvent] array.
Generally the [InputEvent] used is an [InputEventKey], though it can be any [InputEvent], including an [InputEventAction].
2017-09-12 17:42:36 -03:00
</member>
</members>
</class>