- Edited
In my project settings/input map, I have an action called "LD1", which is mapped to the z key on the keyboard. How do I simulate that button being pressed, and then released, in code? Thanks!
In my project settings/input map, I have an action called "LD1", which is mapped to the z key on the keyboard. How do I simulate that button being pressed, and then released, in code? Thanks!
func _input(event):
if event.is_action_pressed("LD1"):
print("z key pressed")
elif event.is_action_released("LD1"):
print("z key released")
You can simulate a button press/release from code using Input.action_press
and Input.action_release
. (I think. I haven’t actually either function before)
@TwistedTwigleg said: You can simulate a button press/release from code using
Input.action_press
andInput.action_release
. (I think. I haven’t actually either function before)
I marked this as an answer, but it actually didn't work. I tried this bit of code, which worked from one script but not from the one I want. Trying to figure out why:
var a = InputEventAction.new()
a.action = "LD1"
a.pressed = true
Input.parse_input_event(a)
I think you just need to do this Input.action_press(“LD1”)
, but I’ll have to check tomorrow. I have never used either function before, so it will be a good chance to learn how it works (as there are times when its useful to press/release actions from code)
Edit: Sorry, I likely will not have time today. I’ll try to look at it soon!
Alright, I have a bit of working code, using Godot 3.0.2 on Windows (10):
if (Input.is_action_just_pressed("ui_select")):
Input.action_press("Test_action")
if (Input.is_action_just_released("ui_select")):
Input.action_release("Test_action")
"------------------------------------------------------------"
" I have no idea why I chose T and D... D for delete, and T for text?"
if (Input.is_action_pressed("Test_action")):
self.text += "T";
if (Input.is_action_just_released("Test_action")):
self.text += "D"
Note: I attached this to a label, which is where self.text
comes from. The one thing I did note is you have to use Input.action_release
or the action will never cease to be released (and T
will be continually added).
@alspacka said: I tried this bit of code, which worked from one script but not from the one I want. Trying to figure out why:
Hmm, I'm not sure. I've had mixed success with making InputEvent
objects when I was working on remapping stuff (though that was back in Godot 3 alpha 1). That is a interesting way of doing it though! I would not have thought about trying something like that for simulating input events from code :smile:
Thanks for the response @TwistedTwigleg, that looks like it will work. I figured out why the one code snippet was working in one script and not another, and it was completely, and embarrassingly, unrelated to my question. When I pressed the play button, I was running my default root node rather than the root node in the new scenetree that I was working in! I didn't realize that was the way GE works.