Highlighted

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

Community Beginner ,
Aug 07, 2020

Copy link to clipboard

Copied

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

Views

58

Likes

Translate

Translate

Report

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

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

Community Beginner ,
Aug 07, 2020

Copy link to clipboard

Copied

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

Views

59

Likes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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();

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 07, 2020 0
Community Beginner ,
Aug 07, 2020

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 07, 2020 0
Most Valuable Participant ,
Aug 07, 2020

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 07, 2020 0
Community Beginner ,
Aug 07, 2020

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 07, 2020 0