Keyboard refactor: move up element check to shorten code elsewhere