Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

ExtendScript question: closing script panel if the user press Esc button down

Explorer ,
Aug 07, 2020 Aug 07, 2020

Hi, I have an ExtendScript question. What I'm trying to is close script window if users press ESC key down. Here's my code for it:

dialog.addEventListener('keydown', escExit);
function escExit() {
try {
var myKeyState = ScriptUI.environment.keyboardState;
var pressedString = myKeyState.keyName;
if (myKeyState.keyName == "Escape") {
this.close();
return;
}
}catch (err) {}
}

When I press Esc it closes the window but when I run the script again, sometimes I get this error message. "Unable to execute script at line 5. Execution halted". It refers to "this.close()" line when I change the code "//this.close()" this time it gives an error for the upper line. If I do the same thing for the new line it gives an error for other upper lines. It completely doesn't make any sense to me.

Do you have a better way to do that?

TOPICS
Error or problem , How to , Scripting
2.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Aug 07, 2020 Aug 07, 2020

I usually construct my key listeners with a switch/case structure, maybe try it like this? I think it's quite similar to what you did, but this seems to work:

 

 

w = new Window ("dialog",undefined);
var myText = w.add('statictext {text:"some text"}');

w.addEventListener ("keydown", function (kd) {pressed (kd)});
function pressed (k) {
try {
switch (k.keyName)
{
case "Up": alert("Up!"); break;
case "Escape": w.close();
}
}catch (err) {}
}

w.center();
w.show();

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 07, 2020 Aug 07, 2020

Thanks for your response. I replaced your code but still get the same issue. Here's a screen recording of the issue. All color windows call  #include "../Library.jsx" so basically only one script file runs.

 

https://www.dropbox.com/s/6ydu54ooohvwdt1/FVN7LkESAP.mp4?dl=0

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 07, 2020 Aug 07, 2020

I'm not even sure that you would need such a construct or it makes sense. All dialogs are dismissed when hitting escape by default, assuming there is no lingering code execution still going on. Other than that it may not be smart to use such an already overloaded key as Esc. I'm pretty sure it messes with some internal code that globally tracks this key input. Using another key combo may work better.

 

Mylenium

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 07, 2020 Aug 07, 2020
LATEST

Hi Mylenium, the naming is confusing but this is not a dialog, it's a palette 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines