Attention: Here be dragons
This is the latest
(unstable) version of this documentation, which may document features
not available in or compatible with released stable versions of Godot.
Checking the stable version of the documentation...
Shortcut
继承: Resource < RefCounted < Object
用于绑定输入的快捷键。
描述
Shortcut(快捷键,也叫热键)是 InputEvent 资源的容器,常用于通过 InputEvent 与 Control 元素进行交互。
一个快捷键可以包含多个 InputEvent 资源,因此可以使用多种不同的输入触发某个动作。
示例:使用 Shortcut 资源捕获 Ctrl + S 快捷键:
extends Node
var save_shortcut = Shortcut.new()
func _ready():
var key_event = InputEventKey.new()
key_event.keycode = KEY_S
key_event.ctrl_pressed = true
key_event.command_or_control_autoremap = true # 在 mac 上将 Ctrl 替换为 Command。
save_shortcut.events = [key_event]
func _input(event):
if save_shortcut.matches_event(event) and event.is_pressed() and not event.is_echo():
print("按下了保存快捷键!")
get_viewport().set_input_as_handled()
using Godot;
public partial class MyNode : Node
{
private readonly Shortcut _saveShortcut = new Shortcut();
public override void _Ready()
{
InputEventKey keyEvent = new InputEventKey
{
Keycode = Key.S,
CtrlPressed = true,
CommandOrControlAutoremap = true, // 在 mac 上将 Ctrl 替换为 Command。
};
_saveShortcut.Events = [keyEvent];
}
public override void _Input(InputEvent @event)
{
if (@event is InputEventKey keyEvent &&
_saveShortcut.MatchesEvent(@event) &&
keyEvent.Pressed && !keyEvent.Echo)
{
GD.Print("按下了保存快捷键!");
GetViewport().SetInputAsHandled();
}
}
}
属性
|
方法
get_as_text() const |
|
has_valid_event() const |
|
matches_event(event: InputEvent) const |
属性说明
快捷键的 InputEvent 数组。
通常使用的 InputEvent 是 InputEventKey,尽管也可以是任何 InputEvent,包括 InputEventAction。
方法说明
返回该快捷键的第一个有效 InputEvent 的 String 形式。
bool has_valid_event() const 🔗
返回 events 是否包含有效的 InputEvent。
bool matches_event(event: InputEvent) const 🔗
返回 events 中是否有等于 event 的 InputEvent。比较事件时使用 InputEvent.is_match()。