5 Replies Latest reply on Jul 24, 2013 3:53 PM by Gustavo Del Vechio

    Best way to manage an "Esc" pressed by the user

    Gustavo Del Vechio Level 3

      Hi friends

       

      I normally create dialogs to offer options to the user before executing the script functions. So, having a "Cancel" button in this dialog would suit the need for an user to cancel the operation.

       

      But let´s say the user selected the options in the dialog and pressed button OK to start the execution of the process. The dialog will close and process will start. Suppose it will take 1 minute to complete.

       

      If, inside this 1 minute, the user presses "Esc" key in keyboard, then the execution will stop, but it will display an alert (like an an error) it was not possible to finish the process (sure, he cancelled the operation).

       

      What I want to understand: Is it possible to manage this "cancellation" of the user via the ESC and do not display alerts? For example, write a function that tells...If user presses "ESC", then abort the process and display no dialog.

       

      Would an event listener that reads the keydown "ESC" would be suitable for this task? or is there any simpler or suitable way to do it?

       

      Thank you very much for the tips.

       

      Gustavo.

        • 1. Re: Best way to manage an "Esc" pressed by the user
          Michael L Hale Level 5

          I don't think it is possible with javascript. I don't know about other languages.

           

          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.

          • 2. Re: Best way to manage an "Esc" pressed by the user
            Gustavo Del Vechio Level 3

            . 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.

             

            Gustavo.

            • 3. Re: Best way to manage an "Esc" pressed by the user
              Michael L Hale Level 5

              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?

               

              But good or bad, I don't see a way to handle the esc key in javascript unless a dialog is open which fires the onClose event.

               

              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.

              • 4. Re: Best way to manage an "Esc" pressed by the user
                xbytor2 Level 4

                But good or bad, I don't see a way to handle the esc key in javascript unless a dialog is open which fires the onClose event.

                 

                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.

                • 5. Re: Best way to manage an "Esc" pressed by the user
                  Gustavo Del Vechio Level 3

                  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.

                   

                   

                  Best Regards

                  Gustavo.