how to sumalate a key press

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

hey
so i have that project that need to simulate a sertan key press
does any one know how to do that

:bust_in_silhouette: Reply From: Adam_S

Create an InputEventKey event, define the scancode and pressed state, and use Input.parse_input_event() to feed it to your game.

func simulate_keypress():
    var a = InputEventKey.new()
    a.scancode = KEY_ENTER
    a.pressed = true # change to false to simulate a key release
    Input.parse_input_event(a)

See here for a list with all scancodes.

Thank you bro. it work for me.

Ytachi1000 | 2022-11-06 16:11

I got
Invalid set index ‘scancode’ (on base: ‘InputEventKey’) with value of type ‘int’.
type of error. Any idea why?

That solution was for Godot 3. Use keycode in Godot 4.