Skip to main content
StefanStehlik
Inspiring
February 19, 2023
Question

Help with the simple script - keyboard input

  • February 19, 2023
  • 0 replies
  • 226 views

Can you assist me with this script? My goal is to keep a textbox always highlighted. Additionally, when I press the "W" key on my keyboard, I want to delete all the content in the textbox, add the composition width to the textbox, and highlight all the text in the box, so that when I start typing I can easily change and delete all the numbers. 

 

However, I am encountering a problem with the "textOnChanging()" function. The numbers that appear in the textbox are only zeros, and I'm unsure how to highlight the text properly.

 

Here is the code:

 

{
    function template(thisObj)
    {
        var scriptName = "template1";

        function BuildAndShowUI(thisObj)
        {
            // Create and show a floating palette.
            var my_palette = (thisObj instanceof Panel) ? thisObj : new Window("palette", scriptName, undefined, {resizeable:true});
            if (my_palette != null)
            {
                var res =
                    "group { \
                        orientation:'column', alignment:['fill','top'], alignChildren:['left','top'], spacing:5, margins:[0,0,0,0], \
                        introStr: StaticText { text:'Template:', alignment:['left','center'] }, \
                        optsRow: Group { \
                            orientation:'column', alignment:['fill','top'], \
                            widthButton: RadioButton { text:'New Comp Width', alignment:['fill','top'], value: 'true' }, \
                            heightButton: RadioButton { text:'New Comp Height', alignment:['fill','top']}, \
                            text_input: EditText { text:'1.0', alignment:['left','top'], preferredSize:[80,20], active: true }, \
                        }, \
                        cmds: Group { \
                            alignment:['fill','top'], \
                            okButton: Button { text:'Run', alignment:['fill','center'] }, \
                        }, \
                    }";
               
                my_palette.margins = [10,10,10,10];
                my_palette.grp = my_palette.add(res);

                my_palette.addEventListener('keydown', keyPressed);
               
                my_palette.grp.optsRow.text_input.onChanging = textOnChanging;
           
                my_palette.grp.optsRow.text_input.text = firstInputTextValue();
                my_palette.grp.optsRow.text_input.active = true;

                my_palette.onResizing = my_palette.onResize = function () {this.layout.resize();}
               
           
            return my_palette;
        }
    }
   
     function firstInputTextValue() {
        var activeItem = app.project.activeItem;
        var textInsideBox1;
    if ((activeItem == null) || !(activeItem instanceof CompItem)) {    
    } else {
        textInsideBox1 = activeItem.width;
    }
        return textInsideBox1;
    }

         function textOnChanging() {    
            var enteredValue = my_palette.grp.optsRow.text_input.text;  
           if (isNaN(enteredValue) || (enteredValue < 0)) {
            my_palette.grp.optsRow.text_input.text = "0";      
            };
        }

        function keyPressed() {
            var myKeyState = ScriptUI.environment.keyboardState;
            try {
             switch (myKeyState.keyName)
             {
             case "W":  
                         my_palette.grp.optsRow.widthButton.value = true;
                         my_palette.grp.optsRow.text_input.text = inputText();

                       break;
             case "H":
                         my_palette.grp.optsRow.heightButton.value = true;
                         my_palette.grp.optsRow.text_input.text = inputText();
                        break;
             case "Escape": my_palette.close(); break;
            }
         } catch (err) {}
     }

     function inputText() {
        var activeItem = app.project.activeItem;
        var textInsideBox;
        if ((activeItem == null) || !(activeItem instanceof CompItem)) {
        } else {
            if (my_palette.grp.optsRow.scaleButton.value) {
                textInsideBox = 1.0;
            }
            else if (my_palette.grp.optsRow.widthButton.value) {
                textInsideBox = activeItem.width;
            }
            else if (my_palette.grp.optsRow.heightButton.value) {
                textInsideBox = activeItem.height;
            }
            else {}
        }
        return textInsideBox
    }
       
        var my_palette = BuildAndShowUI(thisObj);
        if (my_palette != null) {
            if (my_palette instanceof Window) {
                my_palette.center();
                my_palette.show();
            } else {
                my_palette.layout.layout(true);
                my_palette.layout.resize();
            }
        } else {
            alert("Could not open the user interface.", scriptName);
        }
    }
   
   
    template(this);
}

 

This topic has been closed for replies.