@Chrisir Clipboard functionality

fixed,

saw another post about this exact thing on the forum, here is the complete code. Works in P2D AND FX2D, not sure about other renderers.

thread in question:

//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
//https://discourse.processing.org/t/answered-how-to-get-text-from-paste-buffer/1900
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 clipBoard = "000";
int toggle;
String getTextFromClipboard (){
  String text = (String) getFromClipboard(DataFlavor.stringFlavor);
  return text;
}

void setup() {
  size(660, 660,FX2D);
  
}
 
void draw() {
  //
  //clipBoard = getTextFromClipboard();
  background(0); 
  textAlign(CENTER);
  text("Hit space bar to paste from clipboard (Win 10, processing 3.3.7)", width/2, 26);
  text(clipBoard, width/2, 66);
}
 
// --------------------------------------------------------------
 
void keyPressed() {
  if (keyCode== 17){
    toggle=1;
  }
    
  if (key== 'V'&& toggle==1) {
    fill(255);
    text("hello",10,10);
    clipBoard = getTextFromClipboard ();
  }
};
void keyReleased(){
  if (keyCode== 17){
    toggle = 0;
  }
}

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