使用java代码获取系统剪切板中的内容, 直接上源码: 如觉得源码有意思, 请点赞转发.
package cn.example;
import java.awt.*;
import java.awt.datatransfer.*;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException, UnsupportedFlavorException {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
String[] strings = new String[2];
int i = 0;
strings[i] = (String) clipboard.getData(DataFlavor.stringFlavor);
while (true) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
i = (i + 1) % 2;
strings[i] = (String) clipboard.getData(DataFlavor.stringFlavor);
if (!strings[0].equals(strings[1])) {
System.out.println(strings[i]);
}
if ("over".equals(strings[i])) {
break;
}
}
}
}
|