Improve my keyboard logic according to some cool bits from the keyboard-shortcuts branch, plus handle leaving and re-entering the window better.