Refactored KeyPressHandler to avoid class creation overhead.