1.鼠标事件
鼠标的设置
public override void _Ready()
{
Input.MouseMode = Input.MouseModeEnum.Disabled; // 禁用鼠标模式,鼠标光标不可见且无法移动,鼠标输入无效
}
public override void _Ready()
{
Input.MouseMode = Input.MouseModeEnum.Visible; // 显示鼠标光标
}
public override void _Ready()
{
Input.MouseMode = Input.MouseModeEnum.Hidden; // 隐藏鼠标光标,但鼠标仍可移动
}
public override void _Ready()
{
Input.MouseMode = Input.MouseModeEnum.Captured; // 锁定鼠标模式,鼠标光标会被锁定在窗口中心且不可见
}
自定义鼠标光标
public override void _Ready()
{
// 加载自定义图像
var cursorImage = (Image) GD.Load("res://custom_cursor.png");
// 设置自定义鼠标光标,形状为箭头,热点为图像左上角
Input.SetCustomMouseCursor(cursorImage, Input.CursorShape.Arrow, Vector2.ZERO);
}
获取鼠标位置
public override void _Ready()
{
// 获取鼠标移动的相对位置
GetNode("CanvasLayer").Connect("input_event", this, nameof(_OnCanvasLayerInputEvent));
}
private void _OnCanvasLayerInputEvent(object viewport, InputEvent @event, Vector2 shape)
{
if (@event is InputEventMouseMotion motionEvent)
{
Vector2 relativePosition = motionEvent.Relative;
GD.Print("鼠标相对移动位置:", relativePosition);
}
}
public override void _Ready()
{
// 获取当前鼠标的绝对位置
Vector2 currentPosition = GetViewport().GetMousePosition();
GD.Print("鼠标当前绝对位置:", currentPosition);
}
检测鼠标事件
public override void _Ready()
{
// 检测鼠标点击事件
GetNode("CanvasLayer").Connect("input_event", this, nameof(_OnCanvasLayerInputEventClick));
}
private void _OnCanvasLayerInputEventClick(object viewport, InputEvent @event, Vector2 shape)
{
if (@event is InputEventMouseButton buttonEvent)
{
if (buttonEvent.Pressed)
{
switch (buttonEvent.ButtonIndex)
{
case (int)ButtonList.Left:
GD.Print("鼠标左键按下");
break;
case (int)ButtonList.Right:
GD.Print("鼠标右键按下");
break;
// 其他按钮处理
}
}
}
}
public override void _Ready()
{
// 检测鼠标移动事件
GetNode("CanvasLayer").Connect("input_event", this, nameof(_OnCanvasLayerInputEventMove));
}
private void _OnCanvasLayerInputEventMove(object viewport, InputEvent @event, Vector2 shape)
{
if (@event is InputEventMouseMotion motionEvent)
{
Vector2 currentPosition = motionEvent.Position;
GD.Print("鼠标移动到位置:", currentPosition);
}
}
2.键盘事件
检测单个按键按下与释放
public override void _Ready()
{
// 检测“空格”键按下
Input.ActionMap["jump"].Connect("pressed", this, () => GD.Print("空格键按下"));
// 检测“空格”键释放
Input.ActionMap["jump"].Connect("released", this, () => GD.Print("空格键释放"));
}
检测多个按键组合
public override void _Ready()
{
// 检测“Ctrl + S”组合键按下
Input.ActionMap["save"].Connect("pressed", this, () => GD.Print("Ctrl + S 按下"));
// 检测“Ctrl + S”组合键释放
Input.ActionMap["save"].Connect("released", this, () => GD.Print("Ctrl + S 释放"));
}
检测按键的长按状态
private float _pressDuration = 0;
private bool _isKeyDown = false;
public override void _Process(float delta)
{
// 检测按键长按状态并累计时长
if (Input.IsActionPressed("action_key"))
{
if (!_isKeyDown)
{
_isKeyDown = true;
_pressDuration = 0;
}
_pressDuration += delta;
GD.Print("按键长按持续时间:", _pressDuration);
}
else
{
_isKeyDown = false;
}
}
动态绑定与解绑按键
public override void _Ready()
{
// 动态绑定按键
Input.MapAction("dynamic_jump", (InputEvent @event) => {
if (@event is InputEventKey keyEvent && keyEvent.Pressed)
{
GD.Print("动态绑定的按键按下");
}
});
// 动态解绑按键
Input.UnmapAction("dynamic_jump");
}
检测键盘文本输入
public override void _Ready()
{
// 检测键盘文本输入
Input.Connect("text_entered", this, (char character) => {
GD.Print("输入文本:", character);
});
}
注:要在项目设置中输入映射才可以使用(千万不要忘记这一步)