ActionScript 3中获取剪贴板中的数据(FP 11)剪贴板、数据、ActionScript、FP

2023-09-08 15:11:33 作者:浅笑轻吟梦一曲

我想获得剪贴板中的文本在AS3(弯曲,FlashDevelop中)。我知道我不能只是监视剪贴板,因为虽然大多数其他环境可以,Flash应用程序都是邪恶的,可以猜测,当他们看到一个密码,并把它实际上是属于哪个帐户。这就是为什么我听一个MouseEvent.CLICK事件,但看起来它最近改变和Flash仍然说不,不!。这就是为什么我的addEventListener(Event.Paste)到TextField,但看起来像文本字段不会调度这样的事件。

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.

难道我错过了什么,或Adobe的标志色已经是扎根于社会主义?

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.

粘贴在Flash中是用户启动操作一个。它必须由右键单击并选择启动粘贴从内置的上下文菜单或pressing CTRL + V (在Windows上),当的InteractiveObject (除文本字段)是关注的焦点。

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.

如果满足了这些要求, Event.PASTE 将派出,并且监听器里,你可以访问 Clipboard.getData()方法。

If these requirements are met, Event.PASTE will be dispatched, and inside a listener you can access the Clipboard.getData() method.

只有一件事是左,并把我带到这里寻找的解决方案:如何使粘贴按钮,在自定义的上下文菜单中禁用,当有什么在剪贴板?似乎只有一种解决方案 - 定期检查 Clipboard.generalClipboard.formats.length

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;
    }

}
}

如果您要清除剪贴板出于测试目的,您可能会发现错误的解决方案,如在执行这样的命令:%WINDIR%\ SYSTEM32 \ CMD / C回声关|夹。它把一个空字符串到剪贴板并不会禁用上下文菜单中的粘贴选项。相反,(在Windows中)剪切和粘贴文件。这将变灰上下文菜单中的粘贴选项。

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.