how to use two inputs into one output

Why is your one code using if / else and the other code uses switch / case?

My idea is that regardless of the source of the character, you store that in a variable. You're using two variables, key and state; you can make that one variable (e.g. called cmd) if you don't care where it comes from. Next you use that variable to determine what to do.