I'm writing a program that listens to the System Clipboard for changes. The listener runs on a separate thread and performs some action (say, write to file) when the contents of the Clipboard changes.
I'm polling the clipboard using the ClipboardOwner interface, so that when my program loses ownership of the Clipboard (meaning another process has modified the clipboard) an event is fired in my program letting me read the changes.
public class OwnershipClipboardListener extends Thread implements ClipboardOwner
{
private Clipboard clipB = Toolkit.getDefaultToolkit().getSystemClipboard();
public void run()
{
/* Initialize ClipboardListener and gain ownership of clipboard */
}
@Override
public void lostOwnership(Clipboard clipboard, Transferable transferable)
{
/* Auto-fired when I lose Clipboard ownership.
Can do processing and regaining ownership here */
}
}
The problem is, when running in OSX, any change to the clipboard is reflected only if I manually Cmd-Tab to the running process icon in the dock. So if there multiple clipboard operations before I switch to the dock icon, only the last one has any effect. I don't face this issue on Linux or Windows.
It's like the thread goes to sleep when the program loses focus, but the last Event trigger still fires when it wakes up. Is there any way I can prevent this sleep?
Seems Keith is right. However, you can do a workaround by sending the application to the background (on *Nix):
This opens the Java application in the background and does not need window focus for notifications to fire.
I suspect that OSX doesn't provide notification of clipboard changes, so Java is doing the best it can by notifying you whenever it gets woken for some other reason.
My suspicion comes from the NSPasteboard docs, the
changeCount
routine in particular. It says "You can therefore record the change count at the time that you take ownership of the pasteboard and later compare it with the value returned from changeCount to determine whether you still have ownership." No mention of using an event to detect changes.I'm trying to use this workaround however it don't do the trick for me, ideas? Note that there is a bug in Mac OS X port of java: http://java.net/jira/browse/MACOSX_PORT-511
This is my source code: