refactoring of keyboard control