mirror of
https://github.com/godotengine/godot-docs.git
synced 2025-12-31 17:49:03 +03:00
added some c# input examples (#3158)
This commit is contained in:
@@ -31,17 +31,34 @@ Examples:
|
||||
.. tabs::
|
||||
.. code-tab:: gdscript GDScript
|
||||
|
||||
# input event - runs when the input happens
|
||||
func _input(event):
|
||||
if event.is_action_pressed("jump"):
|
||||
jump()
|
||||
|
||||
# polling - runs every frame
|
||||
func _physics_process(delta):
|
||||
if Input.is_action_pressed("move_right"):
|
||||
# move as long as the key/button is pressed
|
||||
# Move as long as the key/button is pressed.
|
||||
position.x += speed * delta
|
||||
|
||||
.. code-tab:: csharp
|
||||
|
||||
public override void _Input(InputEvent inputEvent)
|
||||
{
|
||||
if (inputEvent.IsActionPressed("jump"))
|
||||
{
|
||||
Jump();
|
||||
}
|
||||
}
|
||||
|
||||
public override void _PhysicsProcess(float delta)
|
||||
{
|
||||
if (Input.IsActionPressed("move_right"))
|
||||
{
|
||||
// Move as long as the key/button is pressed.
|
||||
position.x += speed * delta;
|
||||
}
|
||||
}
|
||||
|
||||
This gives you the flexibility to mix-and-match the type of input processing
|
||||
you do.
|
||||
|
||||
@@ -64,6 +81,13 @@ attach the following script:
|
||||
func _input(event):
|
||||
print(event.as_text())
|
||||
|
||||
.. code-tab:: csharp
|
||||
|
||||
public override void _Input(InputEvent inputEvent)
|
||||
{
|
||||
GD.Print(inputEvent.AsText());
|
||||
}
|
||||
|
||||
As you press keys, move the mouse, and perform other inputs, you'll see each
|
||||
event scroll by in the output window. Here's an example of the output:
|
||||
|
||||
@@ -102,6 +126,15 @@ avoid this, make sure to test the event type first:
|
||||
if event is InputEventMouseButton:
|
||||
print("mouse button event at ", event.position)
|
||||
|
||||
.. code-tab:: csharp
|
||||
|
||||
public override void _Input(InputEvent inputEvent)
|
||||
{
|
||||
if (inputEvent is InputEventMouseButton mouseEvent)
|
||||
{
|
||||
GD.Print($"mouse button event at {mouseEvent.Position}");
|
||||
}
|
||||
}
|
||||
|
||||
InputMap
|
||||
--------
|
||||
@@ -129,6 +162,16 @@ the action you're looking for:
|
||||
if event.is_action_pressed("my_action"):
|
||||
print("my_action occurred!")
|
||||
|
||||
.. code-tab:: csharp
|
||||
|
||||
public override void _Input(InputEvent inputEvent)
|
||||
{
|
||||
if (inputEvent.IsActionPressed("my_action"))
|
||||
{
|
||||
GD.Print("my_action occurred!");
|
||||
}
|
||||
}
|
||||
|
||||
Keyboard events
|
||||
---------------
|
||||
|
||||
@@ -145,6 +188,19 @@ the "T" key:
|
||||
if event.scancode == KEY_T:
|
||||
print("T was pressed")
|
||||
|
||||
.. code-tab:: csharp
|
||||
|
||||
public override void _Input(InputEvent inputEvent)
|
||||
{
|
||||
if (inputEvent is InputEventKey keyEvent && keyEvent.Pressed)
|
||||
{
|
||||
if ((Keylist)keyEvent.Scancode == KeyList.T)
|
||||
{
|
||||
GD.Print("T was pressed");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.. tip:: See :ref:`@GlobalScope_KeyList <enum_@GlobalScope_KeyList>` for a list of scancode
|
||||
constants.
|
||||
|
||||
@@ -168,6 +224,21 @@ different when it's "Shift+T":
|
||||
else:
|
||||
print("T was pressed")
|
||||
|
||||
.. code-tab:: csharp
|
||||
|
||||
public override void _Input(InputEvent inputEvent)
|
||||
{
|
||||
if (inputEvent is InputEventKey keyEvent && keyEvent.Pressed)
|
||||
{
|
||||
switch ((KeyList)keyEvent.Scancode)
|
||||
{
|
||||
case KeyList.T:
|
||||
GD.Print(keyEvent.Shift ? "Shift+T was pressed" : "T was pressed");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.. tip:: See :ref:`@GlobalScope_KeyList <enum_@GlobalScope_KeyList>` for a list of scancode
|
||||
constants.
|
||||
|
||||
@@ -198,6 +269,24 @@ also counts as a button - two buttons, to be precise, with both
|
||||
if event.button_index == BUTTON_WHEEL_UP and event.pressed:
|
||||
print("Wheel up")
|
||||
|
||||
.. code-tab:: csharp
|
||||
|
||||
public override void _Input(InputEvent inputEvent)
|
||||
{
|
||||
if (inputEvent as InputEventMouseButton mouseEvent && mouseEvent.Pressed)
|
||||
{
|
||||
switch ((ButtonList)mouseEvent.ButtonIndex)
|
||||
{
|
||||
case ButtonList.Left:
|
||||
GD.Print($"Left button was clicked at {mouseEvent.Position}");
|
||||
break;
|
||||
case ButtonList.WheelUp:
|
||||
GD.Print("Wheel up");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Mouse motion
|
||||
~~~~~~~~~~~~
|
||||
|
||||
@@ -230,6 +319,38 @@ node:
|
||||
# While dragging, move the sprite with the mouse.
|
||||
$Sprite.position = event.position
|
||||
|
||||
.. code-tab:: csharp
|
||||
|
||||
public override void _Input(InputEvent inputEvent)
|
||||
{
|
||||
var sprite = GetNodeOrNull<Sprite>("Sprite");
|
||||
if (sprite == null)
|
||||
return;// No suitable node was found.
|
||||
|
||||
if (inputEvent is InputEventMouseButton mouseEvent && (ButtonList)mouseEvent.ButtonIndex == ButtonList.Left)
|
||||
{
|
||||
if ((mouseEvent.Position - sprite.Position).Length() < clickRadius)
|
||||
{
|
||||
// Start dragging if the click is on the sprite.
|
||||
if (!dragging && mouseEvent.Pressed)
|
||||
dragging = !dragging;
|
||||
}
|
||||
// Stop dragging if the button is released.
|
||||
if (dragging && !mouseEvent.Pressed)
|
||||
{
|
||||
dragging = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (inputEvent is InputEventMouseMotion motionEvent)
|
||||
{
|
||||
// While dragging, move the sprite with the mouse.
|
||||
sprite.Position = motionEvent.Position;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Touch events
|
||||
------------
|
||||
|
||||
|
||||
Reference in New Issue
Block a user