Skip to main content
August 4, 2011
Question

findPreviousWordBoundary and enter key

  • August 4, 2011
  • 2 replies
  • 975 views

these code work great
and it get the last word

but when i use enter key
it's go wrong
and stop getteng last word

remark:some vaiable it for my program

when i comment the  findPreviousWordBoundary    line
it get all text


    <fx:Script>
        <![CDATA[
            import flashx.textLayout.elements.ParagraphElement;
            protected function togglebutton1_clickHandler(event:MouseEvent):void
            {
                var urlLoader:URLLoader = new URLLoader();
                var suggestions:Array = new Array();
                var suggested:Array = new Array();
                var textfields:Array = new Array();
                var format:TextFormat = new TextFormat();
                var currentSelection:int = -1;
               
               
                mytextflow.addEventListener(KeyboardEvent.KEY_UP, suggest);
           
                function suggest(event:KeyboardEvent):void
                {
                    suggested = [];
                   
                   
                     var activePos:int = mytextflow.selectionActivePosition 
                    var curParagraph:ParagraphElement = mytextflow.textFlow.findLeaf(activePos).getParagraph();
                    var wordBoundary:int = curParagraph.findPreviousWordBoundary(activePos);
                //    var lastword:String = curParagraph.getText(wordBoundary,activePos);
                            }               
            }
                       
        ]]>
    </fx:Script>
   
   
    <s:ToggleButton  x="21" y="10"     click="togglebutton1_clickHandler(event)"/>
    <s:RichEditableText id="mytextflow" x="111" y="43" width="363" height="285" backgroundColor="#C39191"/>              

   
</s:Application>

This topic has been closed for replies.

2 replies

August 5, 2011

i found anothe solution but i

hate it

but it work great

function suggest(event:KeyboardEvent):void
                {
                                   
                var activePos:int = mytextflow.selectionActivePosition;
               
                var whitespace:RegExp = /(\t|\n|\s{2,})/g;
                var trimmedValue:String = mytextflow.text.replace(whitespace, " ");
                var space_pos:int  = trimmedValue.lastIndexOf(" ",activePos-1);
                var lastword:String = mytextflow.text.substring(space_pos+1,activePos);
                   
                lbl.text=lastword;
                
                }

Adobe Employee
August 4, 2011

To solve the problem, I think you can add some code to avoid Enter KeyUp event triggering your KeyUp handler.

Reason:

If you use a debug version player, you will find that when you release enter key, you get a player runtime error


RangeError: Error #2006: The supplied index is out of bounds.
at flash.text.engine::TextBlock/findPreviousWordBoundary()
at flashx.textLayout.elements::ParagraphElement/findPreviousWordBoundary()

Line 534 is  "return getTextBlock().findPreviousWordBoundary(relativePosition);". It is a FTE (Flash Text Engine, which is in the player runtime) call, which calls findPreviousWordBoundary(relativePosition) in TextBlock class.

The point is that when you press enter key, you actually create a new paragraph. The TLF recompose process and keyup handler process happen at the same time, and there is a conflict that causes the runtime error.

August 4, 2011

thanks  Jin-Huang to your reply

how can i avoid Enter KeyUp event

i add these code

if(event.keyCode == Keyboard.ENTER)
                    {
                        mytextflow.addEventListener(KeyboardEvent.KEY_UP, suggest);
                    }

but not work

is there another way to get last word while i type  even after enter

Adobe Employee
August 4, 2011

I mean

function suggest(event:KeyboardEvent):void{

     if(event.keyCode != Keyboard.ENTER)
     {
           var activePos:int = mytextflow.selectionActivePosition 
           var curParagraph:ParagraphElement = mytextflow.textFlow.findLeaf(activePos).getParagraph();
           var wordBoundary:int = curParagraph.findPreviousWordBoundary(activePos);
           var lastword:String = curParagraph.getText(wordBoundary,activePos);
      }              

}