Fix keyboard event handling for stacked modal dialogs

This pull attempts to fix keyboard event handling in case multiple modal dialogs are shown at once by maintaining a stack of key-down handler functions that corresponds to the stack of dialogs, and enabling only the handler for the top dialog. Escape-key dismissal is also now handled explicitly in the key-down handler.

Lightly tested with the Extension Manager -> Install Extension dialog stack .

This pull addresses issue #3505 and supersedes pull #3522.