Refactored keyPressed to solve nested focus issues