+7 votes

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.

in Engine by (49 points)

2 Answers

+12 votes
Best answer
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.

by (1,119 points)
selected by

Thanks -- this is helpful. When I hear "scancode" I think of something very different:
raw hardware key IDs like in https://en.wikipedia.org/wiki/Scancode. 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 http://docs.godotengine.org/en/stable/classes/class_inputevent.html#class-inputevent-is-echo

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.

+2 votes

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 KEYS or KEYENTER or KEYKP5 for 5 on the numpad, and lots more.

For more keyboard buttons to use, check this link out and scroll down:
http://docs.godotengine.org/en/3.0/classes/[email protected]

Hope this helps.

by (58 points)

You don't need to set_process_input in Godot 3.0, it detects it automatically.

That's convenient. Thanks!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.