Move key codes to end of file.
Simplify function.