We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Pasting images from the clipboard into Processing
Page Index Toggle Pages: 1
Pasting images from the clipboard into Processing (Read 1398 times)
Pasting images from the clipboard into Processing
Apr 30th, 2010, 6:21am
 
Is it possible to paste an image from the clipboard into Processing ?
Re: Pasting images from the clipboard into Processing
Reply #1 - Apr 30th, 2010, 6:23am
 
Yes.
Re: Pasting images from the clipboard into Processing
Reply #2 - Apr 30th, 2010, 7:13am
 
Done:
Code:
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;

String pastedMessage;
PImage pastedImage;

void setup()
{
size(800, 200);
PFont f = createFont("Arial", 12);
textFont(f);
}

void draw()
{
background(255);
if (pastedImage != null)
{
image(pastedImage, 5, 5);
}
if (pastedMessage != null)
{
fill(#008800);
text(pastedMessage, 5, 15);
}
}

void keyPressed()
{
if (key == 0x16) // Ctrl+v
{
pastedMessage = GetTextFromClipboard();
pastedImage = GetImageFromClipboard();
}
}

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

PImage GetImageFromClipboard()
{
PImage img = null;
java.awt.Image image = (java.awt.Image) GetFromClipboard(DataFlavor.imageFlavor);
if (image != null)
{
img = new PImage(image);
}
return img;
}

Object GetFromClipboard(DataFlavor flavor)
{
Clipboard clipboard = getToolkit().getSystemClipboard();
Transferable contents = clipboard.getContents(null);
Object obj = null;
if (contents != null && contents.isDataFlavorSupported(flavor))
{
try
{
obj = contents.getTransferData(flavor);
}
catch (UnsupportedFlavorException exu) // Unlikely but we must catch it
{
println("Unsupported flavor: " + exu);
//~ exu.printStackTrace();
}
catch (java.io.IOException exi)
{
println("Unavailable data: " + exi);
//~ exi.printStackTrace();
}
}
return obj;
}
Re: Pasting images from the clipboard into Processing
Reply #3 - Apr 30th, 2010, 10:45am
 
Uh great!
What caught my eye was the use of (key == 0x16) to catch CTRL+V
Can you elaborate a litte on how one used this for other "combined" keys? That would also surely make a nice exmple for the "example" section of the package!
Re: Pasting images from the clipboard into Processing
Reply #4 - Apr 30th, 2010, 11:34am
 
Thanks Philho.

I am trying to find out how Processing can keep track of the keystrokes outside of Processing even when i have another application in use, say notepad for example.

Any ideas ?
Re: Pasting images from the clipboard into Processing
Reply #5 - Apr 30th, 2010, 12:59pm
 
Short answer; You can't.
Long answer; http://forums.sun.com/thread.jspa?threadID=678377

So it's not allowed (as far as I know)
Re: Pasting images from the clipboard into Processing
Reply #6 - Apr 30th, 2010, 4:36pm
 
I got Processing to do it, by mistake actually and I can't seem to resimulate it.

It would have to be considered a hack. If I manage to find out what I did I will post what I finally did to get it to work here.
Page Index Toggle Pages: 1