Listen for keydown instead of keyup on non-character keys

This means that preventDefault() works usefully to stop a surrounding form from
submitting before selection is completed.

Also includes some more subjective style improvements...