func _ready():
set_process_input(true)
##This may or may not be required for inputs to work.
func _input(ev):
if Input.is_key_pressed(KEY_K):
do_your_thing()
I don’t really know how to use the ev thing.
You can do KEY_S or KEY_ENTER or KEY_KP_5 for 5 on the numpad, and lots more.
For more keyboard buttons to use, check this link out and scroll down:
Hope this helps.
You don’t need to set_process_input in Godot 3.0, it detects it automatically.
func _input(ev):
if ev is InputEventKey and ev.scancode == KEY_K:
#code
or
func _input(ev):
if Input.is_key_pressed(KEY_K):
#code
or if you want to run the code only once per press
func _input(ev):
if ev is InputEventKey and ev.scancode == KEY_K and not ev.echo:
#code
I do recommend using the input map though. Much easier.
Thanks – this is helpful. When I hear “scancode” I think of something very different:
raw hardware key IDs like in Scancode - Wikipedia. So I skipped over that, my mistake.
Just to be clear: is the difference between your first version (checking the InputEvent) and the second (Input.is_key_pressed) that the first one will only fire once, when the key is actually pressed (and again when it autorepeats), and the second will fire when any input event comes in (even mouse/touch/joystick), as long as the key is currently pressed?
Also, does echo actually mean it’s an autorepeat? (Rather than the actual keypress)
Yes, everything you just said is correct. If you move your mouse for example the Input.is_key_pressed will fire your code more often as long as you press the key. And echo is in fact an ‘autorepeat’. It is better described here.
Particularly, I find _input is often unnecessary, I mostly check inputs in the physics_process, because it’s not only much easier and prettier, but some inputs like the mouse do not echo, so it’s really hard to know if a player is still shooting for example if you use _input.
I know this post is old but it is the first that comes up when Googling this exact issue.
Easy and clean way to do it is to make an action in a project:
Project → Project Settings → Input Map
Add for example “Jump”, bind it to Space and then in the script:
if Input.is_action_just_pressed("Jump"):
do_the_jump()