Newbie: how to get a key press event

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

How can I check for the user pressing the “K” or “S” keys? Not mapped, just raw named keys. This is in Godot3. I have this:

func _unhandled_input(ev):
  if (ev is InputEventKey and ev.??? == "K"):

This seems like it should be simple, I’m probably missing something obvious.

1 Like
:bust_in_silhouette: Reply From: Sdhy

Here’s how I do it:

func _ready():
    ##This may or may not be required for inputs to work.

func _input(ev):
    if Input.is_key_pressed(KEY_K):

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.

mateusak | 2018-02-12 13:52

That’s convenient. Thanks!

Sdhy | 2018-06-14 16:15

:bust_in_silhouette: Reply From: mateusak
func _input(ev):
	if ev is InputEventKey and ev.scancode == KEY_K:


func _input(ev):
	if Input.is_key_pressed(KEY_K):

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:

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)

If this is all correct, I’ll submit a pull request for some doc updates to InputEvent — Godot Engine (stable) documentation in English

garyo | 2018-02-12 12:53

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.

mateusak | 2018-02-12 13:50

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"):
1 Like