3 Replies Latest reply on Aug 9, 2016 1:56 PM by zertle

    ScriptUI use space bar instead of return button

    zertle Level 2

      Is there anyway to have the space bar act as the return button for a ScriptUI dialog window?  Similar to how the {name: "ok"} works.  Right now the space bar will activate whichever button is highlighted, but ideally I'm trying to get it so that the space bar will always hit 'Yes', regardless of what is highlighted.

       

      function temp() {
          var dlg = new Window('dialog');
          dlg.orientation = 'column';
          
          var myPnl = dlg.add('panel');
          myPnl.orientation = 'row';
          
          var one = myPnl.add('radiobutton', undefined, 'IHP East (&1)' );
          one.value = true;
          one.shortcutKey = '1';
          var two = myPnl.add('radiobutton', undefined, 'IHP West (&2)' );
          two.shortcutKey = '2';
          var three = myPnl.add('radiobutton', undefined, 'Three (&3)' );
          three.shortcutKey = '3';
          
          var btnPnl = dlg.add('panel');
          btnPnl.orientation = 'row';
          
          var yes = btnPnl.add('button', undefined, 'Yes', {name: "ok"} );
          var no = btnPnl.add('button', undefined, 'No' );
          var can = btnPnl.add('button', undefined, 'Cancel' );
          dlg.defaultElement = yes;
          
          yes.onClick = function() {
              dlg.close();
              $.write( "Yes\n" );
          }
          
          no.onClick = function() {
              dlg.close();
              $.write( "No\n" );
          }
          
          can.onClick = function() {
              dlg.close();
              $.write( "Cancel\n" );
          }
          
          dlg.show();
      }
      
      temp();
      

       

      Thanks!

        • 1. Re: ScriptUI use space bar instead of return button
          Silly-V Adobe Community Professional

          I think there's a problem that to use the 'keydown' event you have to have an edittext in there which sees the key entries - and it can't be hidden or anything. Not sure how to work around that, but it may be possible.

          • 2. Re: ScriptUI use space bar instead of return button
            Silly-V Adobe Community Professional

            Aha!  Just kidding, the only issue is that you have to have any element in focus on order for the keypress event to work!
            In my case as long as you click at least 1 element that can be focused (not the statictext), then it does it.

            Interestingly, setting the value and active of the radiobuttons to true does not recognize the focusing.. if done during window construction - but moving those into the onShow function does the trick!

             

            #target illustrator

            function test(){

              var w = new Window("dialog", "Test");

              var s = w.add("statictext", undefined, "Hello!");

              var rg = w.add("group");

              var op1 = rg.add("radiobutton", undefined, "Option 1");

              var op2 = rg.add("radiobutton", undefined, "Option 2");

             

              w.addEventListener("keydown", function(k){

                if(k.keyName == "Space"){

                  alert("Good work!");

                  this.window.close();

                }

              });

              w.onShow = function(){

                op1.value = true;

                op1.active = true;

              };

              w.show();

            };

            test();

            • 3. Re: ScriptUI use space bar instead of return button
              zertle Level 2

              Awesome!  That all makes sense!  Thanks so much!