반응형

키보드와 마우스 입력을 간단히 확인해 보자.

 

노드에 스크립트를 추가한다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using Godot;
 
public partial class Main : Node
{
    // Called when the node enters the scene tree for the first time.
    public override void _Ready()
    {
    }
 
    // Called every frame. 'delta' is the elapsed time since the previous frame.
    public override void _Process(double delta)
    {
        if(Input.IsMouseButtonPressed(MouseButton.Left))
        {
            GD.Print($"Mouse Left Button: {GetViewport().GetMousePosition()}");
        }
        if(Input.IsMouseButtonPressed(MouseButton.Right))
        {
            GD.Print($"Mouse Right Button: {GetViewport().GetMousePosition()}");
        }
        if(Input.IsKeyPressed(Key.Escape))
        {
            GD.Print("Escape Key");
            //GetTree().Quit();
        }
    }
}
 

 

 

마우스와 키보드 입력을 감지하는 코드를 작성한다. IsMouseButtonPressed()와 IsKeyPressed()는 버튼이나 키가 눌려있는 동안 계속해서 true를 반환한다. 눌리거나 떼는 순간만 반응하고 싶다면 아래 함수를 사용한다.

Input.IsActionJustPressed()
Input.IsActionJustReleased()

 

버튼이나 키가 눌리면 Output창에 표시된다.

 

1
2
3
4
5
6
7
8
9
10
using Godot;
 
public partial class Script : Node2D
{
    public override void _Input(InputEvent @event)
    {
        if (@event.IsActionPressed("ui_accept"))
            GD.Print(@event.AsText());
    }
}
 

 

_Input()와 InputEvent를 사용하는 코드를 작성한다. InputEvent.IsActionPressed()는 눌리는 순간 한 번만 반응한다.

 

Input Map에 등록된 목록을 사용할 수 있다.

 

ui_accept로 등록된 Enter키나 Space키가 눌리면 AsText()에 의해 키 이름(Enter/Space)이 출력된다.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
using Godot;
 
public partial class Script : Node2D
{
    public override void _Input(InputEvent @event)
    {
        if (@event is InputEventMouseButton mouseEvent && mouseEvent.Pressed)
        {
            switch (mouseEvent.ButtonIndex)
            {
                case MouseButton.Left:
                    GD.Print(@event.AsText() + " at: " + mouseEvent.Position);
                    break;
                case MouseButton.Right:
                    GD.Print(@event.AsText() + " at: " + GetViewport().GetMousePosition());
                    // mouseEvent.position과 GetViewport().GetMousePosition()는 같은
                    // 결과를 출력한다.
                    break;
                case MouseButton.WheelUp:
                    GD.Print(@event.AsText());
                    break;
            }
        }
    }
}
 

 

이번엔 버튼 동작을 구분하는 코드를 입력한다.

 

버튼 동작과 위치가 표시된다.

 

※ 참고

Input Handling

 

반응형
Posted by J-sean
: