|
@@ -14,13 +14,23 @@ multiple locations, depending on the purpose.
|
|
|
|
|
|
Here is a quick example, closing your game if the escape key is hit:
|
|
|
|
|
|
-::
|
|
|
+.. tabs::
|
|
|
+ .. code-tab:: gdscript GDScript
|
|
|
|
|
|
func _unhandled_input(event):
|
|
|
if event is InputEventKey:
|
|
|
if event.pressed and event.scancode == KEY_ESCAPE:
|
|
|
get_tree().quit()
|
|
|
|
|
|
+ .. code-tab:: csharp
|
|
|
+
|
|
|
+ public override void _UnhandledInput(InputEvent @event)
|
|
|
+ {
|
|
|
+ if (@event is InputEventKey eventKey)
|
|
|
+ if (eventKey.Pressed && eventKey.Scancode == (int)KeyList.Escape)
|
|
|
+ GetTree().Quit();
|
|
|
+ }
|
|
|
+
|
|
|
However, it is cleaner and more flexible to use the provided :ref:`InputMap <class_InputMap>` feature,
|
|
|
which allows you to define input actions and assign them different keys. This way,
|
|
|
you can define multiple keys for the same action (e.g. they keyboard escape key and the start button on a gamepad).
|
|
@@ -29,12 +39,23 @@ and even build a key mapping feature on top of it to allow your game to change t
|
|
|
|
|
|
You can setup your InputMap under **Project > Project Settings > Input Map** and then use those actions like this:
|
|
|
|
|
|
-::
|
|
|
+.. tabs::
|
|
|
+ .. code-tab:: gdscript GDScript
|
|
|
|
|
|
func _process(delta):
|
|
|
if Input.is_action_pressed("ui_right"):
|
|
|
# Move right
|
|
|
|
|
|
+ .. code-tab:: csharp
|
|
|
+
|
|
|
+ public override void _Process(float delta)
|
|
|
+ {
|
|
|
+ if (Input.IsActionPressed("ui_right"))
|
|
|
+ {
|
|
|
+ // Move right
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
How does it work?
|
|
|
-----------------
|
|
|
|
|
@@ -146,7 +167,8 @@ from the game code (a good example of this is detecting gestures).
|
|
|
The Input singleton has a method for this:
|
|
|
:ref:`Input.parse_input_event() <class_input_parse_input_event>`. You would normally use it like this:
|
|
|
|
|
|
-::
|
|
|
+.. tabs::
|
|
|
+ .. code-tab:: gdscript GDScript
|
|
|
|
|
|
var ev = InputEventAction.new()
|
|
|
# set as move_left, pressed
|
|
@@ -154,6 +176,15 @@ The Input singleton has a method for this:
|
|
|
# feedback
|
|
|
Input.parse_input_event(ev)
|
|
|
|
|
|
+ .. code-tab:: csharp
|
|
|
+
|
|
|
+ var ev = new InputEventAction();
|
|
|
+ // set as move_left, pressed
|
|
|
+ ev.SetAction("move_left");
|
|
|
+ ev.SetPressed(true);
|
|
|
+ // feedback
|
|
|
+ Input.ParseInputEvent(ev);
|
|
|
+
|
|
|
InputMap
|
|
|
--------
|
|
|
|