Best way to detect key input?

What’s the best way to trigger something on a key input? So far I’ve just been using if statements:

Which has worked fine, but I’m wondering if there is a more elegant or common way.
Thanks!

Looks pretty elegant to me.

Some of this is dedicated to mobile platforms, but it might be helpful.