some refactoring on key event