[Answered] How to get text from paste buffer?

OS: Windows 10 64bit. Processing 3.3.7.
I am looking for a solution on how to get text from the copy/paste system buffer (aka CTRL+C CTRL+V buffer).
So far I have this code compiled from examples and such from other people:

//This next set of code is dumped from:
//https://forum.processing.org/two/discussion/8950/pasted-image-from-clipboard-is-black
//https://forum.processing.org/two/discussion/17270/why-this-getx-method-is-missing-in-processing-3-1-1
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;


String GetTextFromClipboard (){
  String text = (String) GetFromClipboard(DataFlavor.stringFlavor);
  return text;
}


static final javax.swing.JFrame getJFrame(final PSurface surf) {
  return
    (javax.swing.JFrame)
    ((processing.awt.PSurfaceAWT.SmoothCanvas) //Gives error: ClassCastException: com.jogamp.newt.opengl.GLWindow cannot be cast to processing.awt.PSurfaceAWT$SmoothCanvas
    surf.getNative()).getFrame();
}

Object GetFromClipboard (DataFlavor flavor) {

  Clipboard clipboard = getJFrame(getSurface()).getToolkit().getSystemClipboard();
  Transferable contents = clipboard.getContents(null);
  Object object = null;

  if (contents != null && contents.isDataFlavorSupported(flavor))
  {
    try
    {
      object = contents.getTransferData(flavor);
      println("Clipboard.GetFromClipboard() >> Object transferred from clipboard.");
    }

    catch (UnsupportedFlavorException e1) // Unlikely but we must catch it
    {
      println("Clipboard.GetFromClipboard() >> Unsupported flavor: " + e1);
      //~  e1.printStackTrace();
    }

    catch (java.io.IOException e2)
    {
      println("Clipboard.GetFromClipboard() >> Unavailable data: " + e2);
      //~  e2.printStackTrace();
    }
  }

  return object;
} 

But, when function GetTextFromClipboard() is run, the 19th string, as noted by a really long comment, gives an error:

ClassCastException: com.jogamp.newt.opengl.GLWindow cannot be cast to processing.awt.PSurfaceAWT$SmoothCanvas

What do?

1 Like

Have you tried -

Toolkit.getDefaultToolkit().getSystemClipboard();

btw - normally in Java, methods start lower case - eg. getTextFromClipboard()

2 Likes

I tried that before, and it didn’t work because “Toolkit” couldn’t be recognized. Now I tried it again with:

import java.awt.Toolkit;

And it worked. Thanks!

About the first letter being uppercase, that’s because I copied that code from soneone else.

For anyone else finding this post in the future, here’s the final working code: (with first letters of functions being lowercase)

//This next set of code is dumped from:
//https://forum.processing.org/two/discussion/8950/pasted-image-from-clipboard-is-black
//https://forum.processing.org/two/discussion/17270/why-this-getx-method-is-missing-in-processing-3-1-1
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.Toolkit;

String getTextFromClipboard (){
  String text = (String) getFromClipboard(DataFlavor.stringFlavor);
  return text;
}



Object getFromClipboard (DataFlavor flavor) {

  Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); 
  Transferable contents = clipboard.getContents(null);
  Object object = null;

  if (contents != null && contents.isDataFlavorSupported(flavor))
  {
    try
    {
      object = contents.getTransferData(flavor);
      println("Clipboard.getFromClipboard() >> Object transferred from clipboard.");
    }

    catch (UnsupportedFlavorException e1) // Unlikely but we must catch it
    {
      println("Clipboard.getFromClipboard() >> Unsupported flavor: " + e1);
      //~  e1.printStackTrace();
    }

    catch (java.io.IOException e2)
    {
      println("Clipboard.getFromClipboard() >> Unavailable data: " + e2);
      //~  e2.printStackTrace();
    }
  }

  return object;
} 
3 Likes

good information. Well expaliend.