Handling clipboard events in AIR

So!

If you read that last post, you know about Tyro. Since I designed Tyro to be able to handle clipboard events, I expected it to respond to my cut, copy and paste keystrokes when I put it in an AIR application. It didn’t work. Can you think of why?

If you guessed it had something to do with menus, you’re correct. You seem pretty clever; maybe you want to follow along and try this project at home? Clipboard commands are usually provided through the Edit menu of an application, such as a web browser. Most people have learned through experience to rely upon the common keyboard shortcuts that software developers commonly bind to the clipboard commands, but know this: if your browser’s developer didn’t think to include that overlooked Edit menu, all your absent-minded key tapping would do nothing.

The first issue, then, is to include an Edit menu in your AIR application, and to fill it with NativeMenuItems that represent the standard Cut, Copy and Paste operations. I’ll wait while you do that. Don’t forget those keyboard shortcuts! (You might also want to stick in the Select All command, but remember, it doesn’t fit in the same category as the clipboard commands, so you should insert a separator to keep it apart from the other menu items.)

You'll want to do something along these lines.

Now, what’s next? Remember, our goal here is to reproduce the chain of events that cause clipboard events to work in the Flash Player. We’ll want to dispatch events that use the Event.CUT, Event.COPY, Event.PASTE and Event.SELECT_ALL types. And we’ll want to dispatch them from the current stage focus. You might listen for Event.SELECT typed events from your Edit menu items, and respond in a fashion similar to this:

var clipboardEventType:String;
switch (event.currentTarget) {
case myCutMenuItem: clipboardEventType = Event.CUT; break;
case myCopyMenuItem: clipboardEventType = Event.COPY; break;
case myPasteMenuItem: clipboardEventType = Event.PASTE; break;
case mySelectAllMenuItem: clipboardEventType = Event.SELECT_ALL; break;
}
if (stage.focus && clipboardEventType) stage.focus.dispatchEvent(new Event(clipboardEventType));

That ought to do the trick! If you want to be thorough, you can listen for FocusEvent.FOCUS_IN events on the Stage to determine whether there currently is a clipboard-friendly object in focus, and what kind of object it is; that way, if it’s not something you wish to use with the clipboard, you can disable the Edit menu. This whole method should work for any InteractiveObject that has been carefully written to receive stage focus and handle clipboard events.

Wait, what’s that you say? You say you aren’t using the Tyro class as your text system? You’re using TextFields, the way they taught you in school? And they aren’t responding to your events, even when they have focus?

Well then it sucks to be you, doesn’t it? Don’t sweat it, I’m sure you can work out the copy and paste routines yourself. Have fun!


You can leave a response, or trackback from your own site.

Leave a Reply