How to Read Text from the Clipboard

Read Text from Clipboard

I made a quick example to show how to use the Clipboard class from the System.Windows.Forms namespace. It turns out, that the method needed the [STAThread] method attribute to work. I don't know if that is possible to use in a Unity3D C# script.

[STAThread]
static void Main(string[] args)
{
if (Clipboard.ContainsText(TextDataFormat.Text))
{
string clipboardText = Clipboard.GetText(TextDataFormat.Text);
// Do whatever you need to do with clipboardText
}
}

To learn more about what the attribute is used for, have a look at this question (and more importantly, its answers): What does [STAThread] do?

EDIT:

I did a little bit of digging, and it looks like Unity3D has a wrapper for the System Clipboard. I haven't tried it yet, but it looks like it should work across different operating systems and not just for Windows: GUIUtility.systemCopyBuffer

How to read Android Clipboard (text) when opening an app?

The solution I found was running the code that accesses the clipboard in onWindowFocusChanged() as since Android 10, app needs to have input focus in order to access the clipboard, as @laalto pointed out.

Although that created another problem since I need the code to run only on app launch and not every time activity has focus (change of activities, pausing the app and returning, etc).

My approach to that was to create a static boolean variable within the class, then make that variable true within onCreate() and set an if condition on onWindowFocusChanged() to run the code that interacts with clipboard only if that variable is true.
Once I'm done with accessing the clipboard on onWindowFocusChanged(), I make that variable false.

By doing that, the code is run only on app's launch and not every time MainActivity has focus.

Get readable text only from clipboard

import java.awt.HeadlessException;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

String data = (String) Toolkit.getDefaultToolkit()
.getSystemClipboard().getData(DataFlavor.stringFlavor);

with the getData() Method and the stringFlavor you should get plain text from the clipboard.

If there are weird text in the clipboard, I think, this should be a problem of the program which puts the data in the clipboard.

How to get copied text from JavaScript

tl;dr use Method 3 if you really think that not being honest with the user about what you're doing is justified- it's a good workaround, although many might consider it an exploit.

Looking at the W3 specification (https://www.w3.org/TR/clipboard-apis/#Cases) we can see some insight into why these events (and the still-developing API) exist in the first place. Specifically that copy is there for you to change what was copied in the case of your target not being what the user would actually want to end up on their clipboard, while paste exists to let you handle transfering that data into your application.

Knowing this, we can come to some conclusions:

  • Method 1: The spec does not go into much detail about clipboard security, except for making the intention that implementations should work to protect users. I'm not surprised, therefore, that the copied data is hidden from you; it seems like a sensible decision by the implementers. More than that, looking at the algorithms set-out by the spec, it's quite possible that there is not data in the clipboard yet, as the aim of this event is to allow you to set what should end up their.
  • Method 2: This seems much more the intention of the authors. If an application is going to access the clipboard, it should really get permission from the user. Especially because the clipboard might contain data from outside of your page.
  • Method 3: It's an exploit, but it's hard to see cases where it wouldn't work. From an implementer's perspective it's hard to block- as they would have to check event delegate functions for calls; compared to just 'not making the data readily available'. It's also, arguably, secure enough as the only information you can access is information that is already on your own document.


Related Topics



Leave a reply



Submit