I would like to get clipboard text in AS3 (flex, flashdevelop). I know I can't just monitor clipboard, because while most other environments can, flash application are evil and can guess when they see a password and to what account it actually belongs. That's why I listen to an MouseEvent.CLICK event, but looks like it changed recently and Flash still says "no, no!". That's why I addEventListener ( Event.Paste ) to a TextField, but looks like the TextField doesn't dispatch such an Event.
I tried many solutions over Internet but they just don't work, and documentation regarding clipboard is often outdated.
Do I miss something, or Adobe's logo color has it's root in socialism?
What about the flash.desktop.Clipboard
class. This should be your way to access the clipboard. The class is available since Flash Player 10. With the Clipboard
class you can read data from and write data to the system's clipboard in several predefined formats. Check the API documentation of Clipboard class to get detailed information about it.
After years, I stumbled upon my own question to answer it.
Pasting in Flash is one of User Initiated Actions. It must be initiated by either right-clicking and choosing Paste
from built-in context menu or pressing CTRL+V
combination (on Windows), when an InteractiveObject
(except TextField
) is in focus.
If these requirements are met, Event.PASTE
will be dispatched, and inside a listener you can access the Clipboard.getData()
method.
Only one thing is left, and brought me here in search of the solution: how to make the Paste
button disabled in custom context menu, when there's nothing in the clipboard? It seems there's only one solution - to periodically check the Clipboard.generalClipboard.formats.length
.
package simpletests
{
import flash.display.Sprite;
import flash.display.Stage;
import flash.events.Event;
import flash.events.FocusEvent;
import flash.events.TimerEvent;
import flash.events.MouseEvent;
import flash.ui.ContextMenu;
import flash.ui.ContextMenuItem;
import flash.desktop.Clipboard;
import flash.desktop.ClipboardFormats;
import flash.utils.Timer;
public class PasteTest extends Sprite
{
private var cont:ContextMenu;
public function PasteTest()
{
cont = new ContextMenu ();
cont.hideBuiltInItems ();
cont.clipboardMenu = true;
var timer:Timer = new Timer ( 100 );
this.contextMenu = cont;
stage.focus = stage;
timer.start ();
timer.addEventListener ( TimerEvent.TIMER, onTimer );
stage.addEventListener ( FocusEvent.FOCUS_OUT, onFocusOut );
stage.addEventListener (Event.PASTE, onPaste );
}
private function onTimer ( e:TimerEvent ):void
{
cont.clipboardItems.paste = Clipboard.generalClipboard.formats.length;
}
private function onPaste ( e:Event ):void
{
trace ( Clipboard.generalClipboard.getData(ClipboardFormats.TEXT_FORMAT) );
}
private function onFocusOut ( e:FocusEvent ):void
{
stage.focus = stage;
}
}
}
If you want to clear your clipboard for testing purposes, you might find wrong solutions, like executing a command like this: %windir%\System32\cmd /c "echo off | clip"
. It puts an empty string into the clipboard and doesn't disable the "Paste" option in context menus. Instead (in Windows) cut and paste a file. This will gray out the Paste
option in context menus.