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?