Receiving Event.COPY, Event.PASTE and Event.SELECT_ALL events on Sprites

According to my work on http://www.awesay.com, I needed to create my own TextField. One could argument to use the TextLayoutFramework (TLF) created by Adobe, but for our uses it’s overloaded. We don’t want our users to need to download ~ 0.5 MB to just be able to read one line of text. In addition to this, we may need other parts of the FlexSDK to be downloaded, so we easily end up with a download of 1 MB or more. The Spark SWC has a size of 1.3 MB, what’s just too much.
So I started to create my own TextInputField with not more than 650 lines of code. I also want the user to be able to copy and paste text from and to his Clipboard, so I’ve had a look at the Event.PASTE, etc. events that I needed to listen for.

My first problem with this was that my TextInputField didn’t receive any of these events, so I double checked the listeners, but everything was fine. After some research I found out that I need to set stage.focus to the current TextInputField. I thought it should be easy by just doing

this.stage.focus = this;

The TextInputField still didn’t receive any events though…
So I traced the actual value of stage.focus after setting and I got “null” in my trace. Just wonderful…
After some more research, I actually found a solution at stackoverflow.com.
What you need to do to keep stage.focus on your InteractiveObject is to listen for the FocusEvent.FOCUS_OUT event and reset the stage.focus property.
Finally I got the copy and paste functionality to work as expected by first setting stage.focus to this and then listen for the focusOut event to reset it once:

[ ... ]
private function setStageFocus():void
{
  this.addEventListener(FocusEvent.FOCUS_OUT, this.focusOutHandler);
  this.stage.focus = this;
}
 
 
private function focusOutHandler(evt:FocusEvent):void
{
  this.removeEventListener(FocusEvent.FOCUS_OUT, this.focusOutHandler);
  this.stage.focus = this;
}
 
[ ... ]

Leave a comment

Name: (required)

E-Mail: (required)

Website:

Comment: