Hi,
I'm having troubles getting data from the clipboard on Mac OS X.
What I want to achieve: printing new data that was entered into the clipboard. [if it is text based]
The problem: I'm tracking changes in system clipboard by tracking clipboard ownership changes, my idea works fine on both Windows and Linux [openSuse] however on Mac OS X my app can't print the content of the clipboard until it gets focus.
import java.awt.Toolkit;
import java.awt.datatransfer.*;
import java.io.IOException;
public class ClipboardListener extends Thread implements ClipboardOwner {
Clipboard systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
public void run(){
Transferable selection = systemClipboard.getContents(this);
gainOwnership(selection);
while (true) {}
}
public void gainOwnership(Transferable t){
try {this.sleep(100);}
catch (InterruptedException e) {e.printStackTrace();}
systemClipboard.setContents(t, this);
}
public void lostOwnership(Clipboard clipboard, Transferable contents) {
String s;
try {System.out.println((String) clipboard.getData(DataFlavor.stringFlavor));}
catch (UnsupportedFlavorException e) {}
catch (IOException e) {}
gainOwnership(contents);
}
}
public class myApp {
public static void main(String[] args){
ClipboardListener listener = new ClipboardListener();
listener.start();
}
}
What I'm missing/doing wrong?
Edited by: 929040 on Apr 20, 2012 2:21 AM