Refactored the keyEventHandler code (to smaller indentation)