refactorized key listener