Magic numbers are quite annoying
Listen for keydown instead of keyup

This means we can preventDefault() to stop a surrounding form from
submitting before selection is completed.
Code corrections
Minor code restructure

Less nesting!