Flash: addEventListener not detecting Paste events in Input Text boxes
-
21-08-2019 - |
Question
I'm new to Flash, and I can't seem to do this simple action.
(I'm using ActionScript 3.0)
I created an input text box in my editor. The instance name is "test". In my Action editor I have this:
import flash.events.Event;
this.test.addEventListener(Event.PASTE, pasteHandler);
function pasteHandler(e:Event)
{
trace("blaaaaaaaaaagh");
}
When I run it, it doesn't detect any of my paste events, whether I'm doing Ctrl+V or right-click+paste. If I use another event, like MouseEvent.CLICK, it detects it fine. In fact, I don't think any Event.XXX events (like COPY, INIT, etc.) are detected (at least from the ones I tried). All of the MouseEvent and KeyboardEvent events seem to work fine.
What am I doing wrong?
Solution
TextField objects do not dispatch clear, copy, cut, paste, or selectAll events. Sorry for the bad news!
Tyler.
OTHER TIPS
I'd listen for Event.CHANGE since pasting is going to change the field.
The solution to this is to listen for TextEvent.TEXT_INPUT and prevent the default behavior:
textField.addEventListener(TextEvent.TEXT_INPUT, onTextInput );
function onTextInput( e:TextEvent ) : void
{
// this takes place of PASTE because Event.PASTE is not triggered on a textfield
var t:TextField = TextField( e.currentTarget );
t.replaceSelectedText( e.text );
e.preventDefault();
}