Paste and Copy text

edited March 2018 in How To...

Can i paste or copy the text from computer? When i copy some text CTRL+C and can i paste to Processing app (CTRL+V)?


  • Do you mean the editor?

  • No i mean a text from keyboard..

  • edited March 2018 Answer ✓


    here you can get text data from the Win 10 clipboard inside your running sketch (you paste into the variable a1)

    use space bar for ctrl-v (does run in P2D, not in P3D afaik)


    // https: //
    // https: //
    import java.awt.datatransfer.Clipboard;
    import java.awt.datatransfer.Transferable;
    import java.awt.datatransfer.DataFlavor;
    import java.awt.datatransfer.UnsupportedFlavorException;
    String a1=""; 
    void setup() {
      size(660, 660);
      a1 = GetTextFromClipboard();
    void draw() {
      text("Hit space bar to paste from clipboard (Win 10, processing 3.3.7)", width/2, 26);
      text(a1, width/2, 66);
    // --------------------------------------------------------------
    void keyPressed() {
      if (key==' ') {
        a1 = 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)) {
          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);
        catch ( e2)
          println("Clipboard.GetFromClipboard() >> Unavailable data: " + e2);
          e2.printStackTrace() ;
      return object;
    static final javax.swing.JFrame getJFrame(final PSurface surf) {
  • You are not makng any sense. Do you want to copy some text from for example this website and into the processing environment(/text editor)? If that is what you mean, the answer is yes.

    Or do you mean into a sketch you've made? In which case you have to check for whether the user presses some keys.

  • It is not clear what the OP wants but G4P supports copy and paste into the running sketch.

    In this example sketch below click on the text area and press Ctrl + V to paste any text on the clipboard into the area. (You need to copy some text from elsewhere first obviously :D )

    You will need to install G4P using the Contributions Manager for this sketch to run.

    import g4p_controls.*;
    GTextArea txaArea; 
    public void setup(){
      size(320, 200, P2D);
      surface.setTitle("Paste from clipboard example");
      txaArea = new GTextArea(this, 10, 10, 300, 180, G4P.SCROLLBARS_BOTH | G4P.SCROLLBARS_AUTOHIDE);
      txaArea.setPromptText("Use Ctrl + V to paste some text here");
    public void draw(){
Sign In or Register to comment.