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)?

Answers

  • Do you mean the editor?

  • No i mean a text from keyboard..

  • edited March 2018 Answer ✓

    Hello,

    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)

    Chrisir

    // 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 a1=""; 
    
    void setup() {
      size(660, 660);
      a1 = 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(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)) {
        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();
    }
    //
    
  • 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");
      txaArea.setOpaque(true);
    }
    
    public void draw(){
      background(230);
    
    }
    
Sign In or Register to comment.