Is it possible to be informed when clipboard content changes outside of java

不问归期 提交于 2019-12-21 03:30:27

问题


The thing I would want to do is that when the user copies a text in any program (firefox, notepad, pdfReader etc.) my already running java application shall be informed and immediately shows a popup.

I think in order to be able to do this my java application should be invoked whenever system clipboard is changes.

Is that possible with java, if so in which version? I know we can reach and manipulate system clipboard content but my specific question is about invoking the java app. when clipboard content changes.

Thanks


回答1:


I would have naively pretended it's a job for JDIC, but the interwebz told me the truth. So, let me explain a little.

using Toolkit.getSystemClipboard(), you can get access to the native system clipboard. Like any Java object, this clipboard can be listened. Precisely, you can call Clipboard.addFlavorListener(...) to listen to FlavorEvents. What are they ? They're the metal kings ! .... no no no, I completely and utterly digress. Let me come back. So, a FlavorEvent, according to the doc, indicates that

that available DataFlavors have changed in the Clipboard (the event source).

Which may means that Clipboard content has changed. However, I would not go directly on it without a first prototype.

EDIT Example of a prototype from the fingers of AlexR

import java.awt.Toolkit;
import java.awt.datatransfer.FlavorEvent;
import java.awt.datatransfer.FlavorListener;

public class Main {
    public static void main(String[] args) throws Exception { 
        Toolkit.getDefaultToolkit().getSystemClipboard().addFlavorListener(new FlavorListener() { 
            @Override 
            public void flavorsChanged(FlavorEvent e) { 
                System.out.println("changed!!! " + e.getSource() + " " + e.toString()); 
            } 
        }); 
        Thread.sleep(100000L); 
    }
}


来源:https://stackoverflow.com/questions/4802982/is-it-possible-to-be-informed-when-clipboard-content-changes-outside-of-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!