Struggling a bit with some code you posted
original post:
// https: // forum.processing.org/two/discussion/27473/paste-and-copy-text#latest
// 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 clipBoard="";
void setup() {
size(660, 660);
clipBoard = GetTextFromClipboard();
}
void draw() {
//
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 (key==' ') {
clipBoard = GetTextFromClipboard ();
}
}
String GetTextFromClipboard () {
String text = (String) GetFromClipboard(DataFlavor.stringFlavor);
if (text==null)
return "";
return text;
}
Object GetFromClipboard (DataFlavor flavor) {
Clipboard clipboard = getJFrame(getSurface()).getToolkit().getSystemClipboard();
Transferable contents = clipboard.getContents(null);
Object object = null; // the potential result
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;
}
static final javax.swing.JFrame getJFrame(final PSurface surf) {
return
(javax.swing.JFrame)
((processing.awt.PSurfaceAWT.SmoothCanvas)surf.getNative()).getFrame();
}
this bit of code in particular doesnt work with FX2D, is there any workaround?
static final javax.swing.JFrame getJFrame(final PSurface surf) {
return
(javax.swing.JFrame)
((processing.awt.PSurfaceAWT.SmoothCanvas)surf.getNative()).getFrame();
}
1 Like
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