Refactored keypress processing