Tabs in Flash Textfield
19 Mar 2005I want to know if there is any way to use TAB key within a text
field, in Actionscript. I want four space characters to be inserted
inside a text field, whenever a user presses "TAB" button, instead of
selecting the object with next tab index. Can anyone tell me if and
how it is possible?
This question was asked on IndiaMMUG list. I found a quick & dirty solution for this. Yeah, it's achieved using a button and on(keyPress "
Here is my answer:
1) Keep a button in offstage area
2) Put the following code on the button:</p>
on(keyPress "") { if(Selection.getFocus() == targetPath(_txt)) { var ci = Selection.getCaretIndex(); //assuming _txt is instance of textfield var _str:String = _txt.text; var arr:Array = _str.split(""); arr.splice(ci, 0, "\t"); _txt.text = arr.join(""); Selection.setSelection(ci+1, ci+1); delete arr; } }</pre> This is just logic and there is whole big room for improvement and all best practices :)
It might have many bugs, I have not tested it...
Above code works on the cost of *NO* Tabbing in flash movie, which means pressing Tab won't shift focus to different object(movieclip, button, components etc). But I am sure, that can be achieved with more code and better logic. Probably attaching/removing code when Textfield gets/looses focus...
You can:Code beautifier never works because I have not fixed it yet ;)