Because the esc key also can be used to close a dialog, while the dialog is open you can write an onClose event handler to try to add clean up code. It should work if the user only presses the esc key once. But if they press more than once or hold down the esc key the script will stop before the handler is done. In other words the first press closes the dialog( running the onClose event ) and the second press stops the script. Even a keyboard event handler inside the dialog of the esc key can't keep repeated presses from stopping the script. At least I have not found a way and I spent a good bit of time trying.
Once the dialog is closed or if the script doesn't use a dialog the first esc key press stops the script. There isn't an event for esc that can be used by the scripts event manager or app.notifiers. But even if there were I think it would have the same behavior as the dialog onClose.
I can understand why esc stops the script regardless of what the script wants to do. After all the esc key is the user's 'panic button' when they think something is wrong. I am not sure having a way to ingore a user's request to 'stop now' is a good thing.
. After all the esc key is the user's 'panic button' when they think something is wrong. I am not sure having a way to ingore a user's request to 'stop now' is a good thing.
Hi Michael. Tghank you as ever for your reply. About your last line, sorry if my explanation was a little confused. I do not want to stop the user request of "ESC". I want just to manage it:
Explaining better: when user enters in panic and presses ESC button, I´d like just to stop (the normal effect) but display him a dialog: "The operation was cancelled sucessfully". instead of the script automatically stop and displays an error message.
For an user not familiar with scripts, I´m affraid, if he runs the script, try to "ESC" and receive an error message, he can be affraid about any problem that happened. So, if I was able to give him a message "relax, you cancelled sucessfully" would be best than an automatic error.
I think I understood your intent from your first post. And I can understand why you would want to manage the esc requests, I have tried to do so myself.
But in order to manage the esc event you have to keep running the script. That to me that is ignoring the esc key, at least temporally, to run some more code( even a single alert line ). I am sure that most script writers here would only want to do 'good things' like calm the user with a 'relax' message or restore any user preferences, etc the script may have changed. But what is to stop someone from writing something that does 'bad things'( either on purpose or by error ) that the user can't stop?
Outside a dialog there is only ScriptUI.environment.keyboardState available to determine what key the user is pressing. But it is not an keyboard event handler. It only reports the current keyboard state so the user has to be pressing a key when the keyboardState is checked. And it doesn't work if the esc key is press because that key stop the script.
Yep. The only way I've been able to deal with this situation is with a progress pallette, which can be a bit flakey on its own.
Thank you very much Michael and xbytoer2 for the answers.
So there´s not so much to do with. Leave user press ESC key and receive the stop message.