• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Cursor (caret) position within EditText

Contributor ,
May 18, 2015 May 18, 2015

Copy link to clipboard

Copied

Hey guys,

Not sure if this is possible but I'm curious if there is a way to determine the position of the cursor in the EditText?  I know you can insert anything at the point of the cursor (or selection) with EditText.textselection.  What I'm interested in doing is figuring out the position of the cursor so that I can compare the text before the cursor to the text after the cursor.  Basically I want my program to be able to look at inputted text and determine if, when typing in an opening brace ( { ) that there isn't already a closing brace ( } ) and if there isn't, automatically add one.  I want to implement my own version of auto complete with some other custom solutions rolled in.

Thanks!

Calvin

TOPICS
Scripting

Views

1.0K

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
community guidelines
Engaged ,
May 23, 2015 May 23, 2015

Copy link to clipboard

Copied

The onChanging event fires every time you make a keystroke in an EditText control. You could keep a copy of the text in a variable outside the scope of the event function (or a global) and compare the two. The difference would always be one character, so you should be able to find the current position.

This kind of works, but there are probably better ways to find the difference:

{

     var dlg = new Window("dialog", "test");

     var txtEdit = dlg.add("edittext", [10, 10, 500, 100], "", {multiline: true});

     var txtInfo = dlg.add("statictext", [10, 110, 500, 120]);

     var oldText = "";

     txtEdit.onChanging = function() {

          var info = "Cursor is at char position ";

          var pos = 0;

          for(var i=0; i<this.text.length; i++) {

               var chCur = this.text.charAt(i);

               var chOld = oldText.charAt(i);

               if(chCur !== chOld) {

                    pos = i + 1;

                    break;

               }

               if(pos === 0) {

                    pos = this.text.length;

               }

          }

          info += pos.toString();

          txtInfo.text = info;

          oldText = this.text;

     };

 

     dlg.show();

}

Votes

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
community guidelines
Contributor ,
May 27, 2015 May 27, 2015

Copy link to clipboard

Copied

Hey Christian,

This is actually a great solution...definitely out of the box thinking.  It usually takes a brute force solution when a language/architecture leaves out certain useful functionality.  I'm gonna play around with this for sure! 

Calvin

Votes

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
community guidelines
Advocate ,
Jun 02, 2015 Jun 02, 2015

Copy link to clipboard

Copied

EditText elements have a "textselection" attribute that you can use to insert text at cursor point. I use this on my ExtendScript Developer Utility script for the code writing area.

Here's an example showing it in action. Just place your cursor in the second edittext box anywhere and click the Insert button. Also try selecting some text and clicking Insert.

var win = new Window("palette", "Name", undefined);

var et = win.add("edittext", undefined, "New text");

et.characters = 30;

var insert = win.add("button", undefined, "Insert");

var et2 =  win.add("edittext", undefined, "Old text");

et2.characters = 30;

var close = win.add("button", undefined, "Close");

insert.onClick = function(){

  if(et2.textselection.length > 0){     //Checks the length of selected characters

  et2.textselection = et.text + et2.textselection;     //Inserts new text in front of the old selected text

  }else{

  et2.textselection = et.text;     //Simply inserts the new text at the current cursor position

  }

}

win.center();

win.show();

Votes

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
community guidelines
Contributor ,
Jun 03, 2015 Jun 03, 2015

Copy link to clipboard

Copied

LATEST

Thanks David.  I had checked out textselection and unfortunately it doesn't fully cover the functionality I need.  I want to eventually use the textselection to insert but I need to know where I am in the text so I can search forwards and backwards for certain patterns to close.  I think between Christian's idea and yours it'll cover what I need. 

Votes

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
community guidelines