Only close modal if escape was hit with keydown; fixes #13929

See #13929 and also #13855.

Adds two unit tests for this as well.

/cc @fat