Is there an event for when a user presses the esc key and no text input has focus?


#1

I’m attempting to catch when a user presses the ESC key on a dialog but so far I can only capture a key event when the user has a text input focused.

// works if a text input has focus - does not if not
window.addEventListener("keydown", escKeyDownHandler);

escKeyDownHandler(event) {

    if (event.keyCode==27) {
      window.removeEventListener("keydown", this.escKeyDownHandler);
      this.closeDialog();
    }
}

I was falling back on using a promise but avoiding it if possible due to a few reasons.

Update: Using await I’m getting an error.

What I’m doing is adding a new view to the document for some options but if the user escapes out of the dialog using the escape key I get the following error when they open the dialog again. If the user cancels the dialog with the close buttons then I’m able to remove the form before hand there are no errors. I’ve checked if the view is still added to the document and attempted to remove it before opening the dialog again but it seems it’s too late.

Plugin TypeError: Cannot read property ‘has’ of null
at b.value (uxp://uxp-internal/home/ubuntu/jenkins/workspace/Torq/torq-native/release-2.2.0/build/modules_gen/domjs/src/js/domjs_scripts.js:238:3480)
at b.value (uxp://uxp-internal/home/ubuntu/jenkins/workspace/Torq/torq-native/release-2.2.0/build/modules_gen/domjs/src/js/domjs_scripts.js:238:8889)
at b.value (uxp://uxp-internal/home/ubuntu/jenkins/workspace/Torq/torq-native/release-2.2.0/build/modules_gen/domjs/src/js/domjs_scripts.js:238:3863)
at uxp://uxp-internal/home/ubuntu/jenkins/workspace/Torq/torq-native/release-2.2.0/build/modules_gen/domjs/src/js/domjs_scripts.js:198:713
at Array.map ()
at h (uxp://uxp-internal/home/ubuntu/jenkins/workspace/Torq/torq-native/release-2.2.0/build/modules_gen/domjs/src/js/domjs_scripts.js:198:668)
at b.set (uxp://uxp-internal/home/ubuntu/jenkins/workspace/Torq/torq-native/release-2.2.0/build/modules_gen/domjs/src/js/domjs_scripts.js:198:3599)
at b.set (uxp://uxp-internal/home/ubuntu/jenkins/workspace/Torq/torq-native/release-2.2.0/build/modules_gen/domjs/src/js/domjs_scripts.js:198:2357)
at updateDialogValues (/Users/user/Library/Application Support/Adobe/Adobe XD CC/develop/plugin/main.js:2072:34)
at showDialogWindow (/Users/user/Library/Application Support/Adobe/Adobe XD CC/develop/plugin/main.js:2515:3)


#2

Dialogs will fire a variety of events:

  • close
  • cancel

You can use cancel to see when ESC is triggered.

Alternatively, watch for the return value from showModal, since it will resolve to reasonCanceled if ESC is pressed.