Should I use the change
or textInput
event to capture user input on a TextInput control? Why?
textInput
is dispatched only when the user has input text into the control. change
, on the other hand, is dispatched on every change committed by the user. So for example, if the user deletes a part of the text, only the change
event is dispatched.
Neither of these is dispatched when the text is modified via code:
flash.events.TextEvent.TEXT_INPUT
:
"Dispatched when the user types, deletes, or pastes text into the control."
(ignore the word "delete" there -- I just tried it and this event is not dispatched when text is deleted by the user)
"Dispatched when text in the TextInput control changes through user input. This event does not occur if you use data binding or ActionScript code to change the text."
You can also use the valueCommit
event, which is dispatched when the user "commits" the changes (usually by moving the focus away from the text field), but remember that this event is also dispatched when the field value is changed programmatically.
That's a great answer, hasseg. If I had enough rep, I'd up-vote it.
Depending on what you're capturing the user input for, you could subclass the TextInput component and override the internal listeners for the change and textInput events.
I don't know if there are a lot of reasons you'd want to do this, but I did it recently to deal with a bug in OS X that causes pasted linebreaks to be represented as '\r', instead of '\n'.
All you need to do is add the following after your super() call in the constructor of your subclassed object:
this.addEventListener(Event.CHANGE, textFieldChangeListener);
this.addEventListener(TextEvent.TEXT_INPUT,textFieldInputListener);
And then add the listener methods and the code you want to execute.
Both Event.CHANGE and TextEvent.TEXT_INPUT events trigger on each character typed. If you want an event that will only trigger a single time for a given TextInput field, use FocusEvent.FOCUS_OUT.
Like Event.CHANGE and TextEvent.TEXT_INPUT, this event will only trigger for user input, not for programmatic changes to the value.
CHANGE
doesnt tell you what has changed though - TEXT_INPUT
and KEY_DOWN
do.
© 2022 - 2024 — McMap. All rights reserved.
text
property in the event handler fortextInput
event (usingevent.currentTarget.text
), the value I get reflects the value oftext
before the keypress that triggered the event. – Occupant