Merge "Refactor key handling and selection."