7 June 2011

Flex 4.5 TextArea Enable Tab Key

I was working on a file editor in Flex and I needed to be able to tab within the edit pane without actually tabbing out of the TextArea.

Well there are many ways to achieve this, firstly you could subclass the TextArea or listen for the tab key, but here is the solution I came up with, I find it simple and clearly visible to developers as to what it's doing.



As you can see i'm preventing the default action of the tab key then inserting 4 spaces in its place. Using the insertText method inserts the text at the current cursor position, this is ideal for editing where you wish to insert a tab mid paragraph etc.

Of course if you wanted to actually insert a tab you can just replace the 4 spaces with "\t"

0 comments: