How do i get key enter utilizing c#

How do i get key enter utilizing c#

[ad_1]

Though I imagine you already know the way to examine if an motion is pressed, allow us to have a look at it once more. You’ll be able to, for instance, do that:

Enter.IsActionPressed("Move_Up")

Or this for analog enter:

Enter.GetActionStrength("Move_Up")

Now that String should match an motion configured in undertaking settings. So we’re speaking about studying and writing undertaking settings.

Nonetheless, enter is dealt with a bit completely different than the remaining. There are a pair issues to know:

  • The InputMap offers you entry to the enter settings. So we might be utilizing that.
  • What’s getting saved in undertaking settings are InputEvents. Sure, the category of objects you get in _Input. They’re Useful resources, to allow them to be saved. And what Godot is doing is matching the enter it will get to the saved ones.

Earlier than we go on, I have to level out that manipulating InputMap on the editor (i.e. device scripts) gives you entry to the editor settings as a substitute. And sure, you possibly can find yourself messing them up.


First, to seek out out if a motion is configured we are able to do that:

if (InputMap.has_action("Action_Name"))
{
    GD.Print("The motion exists");
}

Second, if you wish to discover the names of all of the actions which can be configured do:

foreach (var action_name in InputMap.GetActions())
{
    GD.Print(action_name);
}

Third, to get the InputEvents related to an motion, you do that:

foreach (var @occasion in InputMap.ActionGetEvents("My_ActioN"))
{
    GD.Print(@occasion);
}

Fourth, that is what I do to vary them:

InputMap.ActionEraseEvents("My_Action");
foreach (var new_event in my_events)
{
    InputMap.ActionAddEvent("My_Action", new_event);
}

And I remind you that these occasions are InputEvent, so you may get them from _Input and add them to InputMap. You most likely need to filter them to the machine the participant is utilizing (e.g. which controller). And keep in mind leaving a method to cancel or restore the unique settings.


You, after all, will even need to have the ability to save these, and for that I remind you that they’re Useful resources. So you should use ResourceSaver and ResourceLoader.

Nonetheless, in case you relatively not…

  • For keyboard: From InputEventKey you should use both keycode or physical_keycode. And you may additionally need to retailer alt_pressed, ctrl_pressed, shift_pressed and meta_pressed.

  • For mouse:

    • From InputEventMouseButton you utilize button_index.
    • From InputEventMouseMotion… I haven’t got code for this per-se. As an alternative I’ve sensitivity and invert axis.
  • For joystick:

    • From InputEventJoypadButton you utilize button_index.
    • From InputEventJoypadMotion you utilize axis and axis_value (most likely simply the signal).

    Addendumm: In each circumstances you establish the machine by the machine property.

Word: I am wanting on my GDScript code for the property names. I presume the capitalization is completely different on C#.


Bonus: if you wish to mimic enter, there’s Enter.ActionPress and Enter.ActionRelease. There’s additionally Enter.ParseInputEvent which requires an InputEvent. There have been some bugs round these in Godot 4+ (both not doing something or enter getting caught), so take a look at if ParseInputEvent works higher than ActionPress and ActionRelease in your model.

[ad_2]

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply