Paste text from Clipboard using button
I have control bar buttons Copy, Cut, and Paste and they suppose to copy/paste some text/objects from and to clipboard.
Copy/Cut works fine. Paste using CTRL+V and context menu works fine as well.
But when I'm trying to access Clipboard vi开发者_开发问答a control bar button click handler it throws error
SecurityError: Error #2179: The Clipboard.generalClipboard object may only be read while processing a flash.events.Event.PASTE event.
at flash.desktop::Clipboard/getObjectReference()
at flash.desktop::Clipboard/convertNativeFormat()
at flash.desktop::Clipboard/getOriginal()
at flash.desktop::Clipboard/getData()
... 2 more
I know that this is security issue.
Method Clipboard.generalClipboard.getData() may be used to read the contents of the system Clipboard, but only when it is called from within an event handler processing a flash.events.Event.PASTE event.
But I have to implement paste control bar button. Had do I do that?
To overcome this security limitation imposed by Flash Player you can either simulate the functionality of system clipboard with Flash Local shared objects like it was first suggested here http://joeberkovitz.com/blog/2008/12/12/clipboard-using-local-shared-objects/ or use Javascript API via ExternalInterface to retrieve the data from the Clipboard.
The only thing I've been able to find is the ability to paste using a custom context menu. You can find an example on how to do that here. I've tried dispatching "paste" events myself and the player seems to somehow detect that the paste comes from the context menu or keyboard - not entirely sure how it does that or why that is. I hope someone else can provide more clarification, but in the mean time, would a context menu work?
UPDATE: I don't think there's any way to force a paste event. I've been running some tests and it seems the only way to get this event to dispatch and allow the Clipboard.generalClipboard.setContents method to be called without the security error is to respond to a global paste event, which can only be dispatched "safely" from a context menu or keyboard event (ctrl+V or the platform equivalent.) Sorry.
In other programming environment, I have found that triggering a clipboard paste action from a button doesn't work because the button itself gets the focus, away from the target edit control, before the paste action takes place. Could this be this case?
In order to avoid the exception security, the target of the event MUST BE the stage object of the application. I didn't found this information in any documentation, it is from my own experience. Hope it works.
精彩评论