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?
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 pressingCTRL+V
combination (on Windows), when anInteractiveObject
(exceptTextField
) is in focus.If these requirements are met,
Event.PASTE
will be dispatched, and inside a listener you can access theClipboard.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 theClipboard.generalClipboard.formats.length
.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 thePaste
option in context menus.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 theClipboard
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.