听剪贴板更改,检查所有权?

ano*_*001 6 java clipboard

如果将字符串复制到系统剪贴板,我希望收到通知.从同一源应用程序复制新字符串时,FlavorListener将不会获取事件.为了在复制另一个字符串时获得通知,我从剪贴板中读取字符串,将其转换为SrtingSelection,它可以获取所有权,并将其放回剪贴板.一旦StringSelection失去了所有权并且一旦收回它,我就得到了两次通知.有没有办法直接检查所有权,而不是存储字符串并检查它等于新的字符串?到目前为止,这是我的代码:

 import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.FlavorEvent;
import java.awt.datatransfer.FlavorListener;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws Exception {
        // The clipboard
        final Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
        // read clipboard and take ownership to get the FlavorListener notified
        // when the content has changed but the owner has not
        processClipboard(cb);
        cb.addFlavorListener(new FlavorListener() {
            @Override
            public void flavorsChanged(FlavorEvent e) {
                processClipboard(cb);
            }
        });
        // keep thread for testing
        Thread.sleep(100000L);
    }

    public static void processClipboard(Clipboard cb) {
        // gets the content of clipboard
        Transferable trans = cb.getContents(null);
        if (trans.isDataFlavorSupported(DataFlavor.stringFlavor)) {
            try {
                // cast to string
                String s = (String) trans
                        .getTransferData(DataFlavor.stringFlavor);
                System.out.println(s);
                // only StringSelection can take ownership, i think
                StringSelection ss = new StringSelection(s);
                // set content, take ownership
                cb.setContents(ss, ss);
            } catch (UnsupportedFlavorException e2) {
                e2.printStackTrace();
            } catch (IOException e2) {
                e2.printStackTrace();
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望你能理解我的坏英语:-(

Yu *_*hen 5

之前的答案即将开始.

真正的解决方法是只监控所有权变更事件.监视器在监视剪贴板时占用剪贴板作为所有者,因此当任何应用程序更改剪贴板时,所有权将更改,因此这将可靠地指示剪贴板内容更改.但是,在访问剪贴板并重新占用剪贴板之前,所有权更改事件之后,此方法必须有足够的等待工作(发现200毫秒才能正常工作).

该解决方案由marc weber提供并证明有效,网址http://www.coderanch.com/t/377833/java/java/listen-clipboard

我已经验证了我的目的.如果需要,我可以在这里发布解决方案.


小智 1

为了避免双重通知,请在设置新的剪贴板内容之前删除风味侦听器,并在设置剪贴板内容后再次添加侦听器。

public class NewClass implements FlavorListener, ClipboardOwner{
    private Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();  
    public NewClass() {
        System.out.println("NewClass constructor");
        clip.setContents(clip.getContents(null), this);
        clip.addFlavorListener(this);
        try {
            Thread.sleep(100000L);
        }
        catch (InterruptedException e) {

        }
    }

    @Override
    public void flavorsChanged(FlavorEvent e) {
        System.out.println("ClipBoard Changed!!!");
        clip.removeFlavorListener(this);
        clip.setContents(clip.getContents(null), this);
        clip.addFlavorListener(this);

    }

    @Override
    public void lostOwnership(Clipboard arg0, Transferable arg1) {
        System.out.println("ownership losted");
    }
}
Run Code Online (Sandbox Code Playgroud)