Improve keyboard selection code