Store the pressed key in the appropriate container

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Emmanuele

I created my custom action in Project → Project Settings → Input Map → “custom_space”. I called it “ui_movement”, it contains the movement’s keys(up, down, left, right).

This is the code: I want to store the exact value of the pressed key in the variable “direction” or maybe print the value

if Input.is_action_pressed("ui_movement"): 
   direction = the exact pressed key(Is it UP, DOWN, LEFT or RIGHT?)
   or
   print(VALUE OF THE PRESSED KEY)
:bust_in_silhouette: Reply From: kidscancode

Your question is a little bit confusing, and I don’t think you’re understanding how input actions work. You added a new input action to the Input Map. Did you name it "custom_space" or "ui_movement" (not sure why you’d name it “ui” by the way)?

If you added all four directional keys to the same action, then any of them will trigger the action. That’s the purpose of actions - that any number of inputs can be mapped to a single action. is_action_pressed() then tells you that action occurred. If you want to capture separate events for different keys, then you need multiple actions.

You might find it helpful to read the following new tutorial that was recently added to the docs: Input examples — Godot Engine (latest) documentation in English

Sorry for the “customspace” string, I was wrong when I copied the path.
I have a sprite that moves in the four direction.
I asked this question to avoid inserting so many IF constructs.

I was wondering if there was a shorter and more efficient way to write this code. That’s all

Emmanuele | 2019-04-11 01:11

There are four different direction keys, and you’re going to want four different things to happen, so not really. There’s really nothing wrong with four conditional statements in your movement code. Shorter doesn’t always mean more efficient. You can always stick them in their own function and collapse it once you have it set up…

kidscancode | 2019-04-11 01:35