3 Replies Latest reply on Dec 12, 2006 4:22 PM by FlightGuy

    Catching and preventing backspace in a textArea

      Hi all,

      I have a TextArea control in which I need to prevent editing of a subset of the text. This is moderately straightforward for text input, irrespective of whether the user has selected a range or simply placed the caret in some position. To do this, I listen for the "textInput" event, and if it's not to happen I can just do an event.preventDefault() to stop it from happening. The documentation for the TextArea suggests that this event is "Dispatched when the user types, deletes, or pastes text into the control." However I don't see this event being fired on delete.

      Does anyone have a suggestion (other than remembering the contents and resetting them on "change" - not a very clean solution)?
        • 1. Re: Catching and preventing backspace in a textArea
          peterent Level 2
          This appears to be either a bug with the documentation or in the software. I'll file a report. I'm afraid that leaves you with the work-around.
          • 2. Re: Catching and preventing backspace in a textArea
            FlightGuy Level 1
            Thanks Peter - my guess is a bug with the documentation (unfortunately), since the TextField makes no such claim and the TextArea simply delegates to the TextField.

            Just for the benefit of anyone else who tries to do the same, my workaround entails listening both to the keyDown and keyUp events. The keyDown event fires when the delete or backspace key is pressed, but the text value is as yet unmodified. On this event (if it's one of those keys), I capture the textField's text value, selectionBeginIndex and selectionEndIndex. On the keyUp event for one of these keys, I reset the textField's text property, and then call setSelection to restore the selection to whatever it had been. This has the unfortunate consequence of seeing the data disappear and then reappear, but it does allow a reasonably encapsulated way of preventing it.

            I'm interested if anyone has any suggestions to improve it.
            • 3. Catching and preventing backspace in a textArea
              FlightGuy Level 1
              Well... It never is so easy, is it?! If the user presses backspace and holds it down, I don't get an event to react to until they lift the key again. So, it needs to be a little more intelligent than this. In face the keyDown event fires repeatedly if you hold the key down, and the keyUp fires just once when the key is lifted.

              Can anyone confirm that the source for flash.text.TextField is unavailable?