Moved keyboard handling to own function.