0

I'm trying to use Java code in an XPages application to copy text to the local Windows clipboard. My code (executed by clicking a button) copies the code and outputs it to the IBM / Lotus Domino server console. I would like to be able to paste the copied text locally (on my Windows 7 PC) using Ctrl-V but this does not work. What change does my code need to make this work?

import java.awt.datatransfer.*;
import java.awt.Toolkit;
import java.io.*;

        Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();

        StringSelection testData;

        //  Add some test data 

        testData = new StringSelection( "New Test Data" );

        c.setContents(testData, null);

        //  Get clipboard contents, as a String

        Transferable t = c.getContents( null );

        if ( t.isDataFlavorSupported(DataFlavor.stringFlavor) )
        {
            Object o = t.getTransferData( DataFlavor.stringFlavor );
            String data = (String)t.getTransferData( DataFlavor.stringFlavor );
            System.out.println( "Clipboard contents: " + data );
        }

        System.exit(0);

2 Answers2

1

This is not possible with Java. The Java code runs on server and has no possibilities to "fill" client's clipboard.

If you don't depend on IE or older browser versions then use HTML5 Clipboard API in CSJS. (https://stackoverflow.com/a/26336421/2065611 http://datatables.net/blog/2014-01-31#Clipboard-API). Get the text for clipboard from server with XSP.partialRefreshGet and execute ClipboardEvent 'copy' onComplete.

Community
  • 1
  • 1
Knut Herrmann
  • 30,880
  • 4
  • 31
  • 67
  • Ah, the HTML5 Clipboard API. I did not know that. Thanks, Knut :-) – Per Henrik Lausten Mar 03 '15 at 21:03
  • Thank you so much, I will try that and let you know how it went. –  Mar 04 '15 at 07:12
  • I would like to be able to paste text from the clipboard with the Ctrl V key combination but this doesn't work for me. Is this possible with the HTML 5 API? –  Mar 04 '15 at 10:47
1

In addition to Knut Herrmanns answer: it is not possible to copy text to the user's clipboard using for instance client-side Javascript.

However, you can use Flash for this. I have used ZeroClipboard for this in a project.

Update: Knut updated his answer with a reference to the HTML5 Clipboard API. So my answer is only needed if you need to support IE and older browsers - and who isn't :-)

Per Henrik Lausten
  • 21,331
  • 3
  • 29
  • 76