Check updates to clipboard every 200ms

osx
sgoudham 3 years ago
parent 1751661183
commit 075459475f
No known key found for this signature in database
GPG Key ID: EF51A29A50FB754C

@ -4,6 +4,7 @@ import me.goudham.listener.ClipboardEvent;
import me.goudham.model.MyClipboardContent;
import org.jetbrains.annotations.NotNull;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
@ -12,6 +13,9 @@ import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class ClipboardListener extends Thread implements ClipboardOwner {
private final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
@ -25,7 +29,8 @@ public class ClipboardListener extends Thread implements ClipboardOwner {
public void lostOwnership(Clipboard oldClipboard, Transferable oldClipboardContents) {
try {
sleep(200);
} catch (InterruptedException ignored) { }
} catch (InterruptedException ignored) {
}
Transferable newClipboardContents = oldClipboard.getContents(currentThread());
processContents(oldClipboard, newClipboardContents);
@ -43,7 +48,8 @@ public class ClipboardListener extends Thread implements ClipboardOwner {
Image imageContent = (Image) newClipboardContents.getTransferData(DataFlavor.imageFlavor);
clipboardContent = new MyClipboardContent<>(imageContent);
}
} catch (UnsupportedFlavorException | IOException ignored) { }
} catch (UnsupportedFlavorException | IOException ignored) {
}
clipboardEvent.onCopy(clipboardContent);
}
@ -53,9 +59,28 @@ public class ClipboardListener extends Thread implements ClipboardOwner {
}
public void run() {
Transferable currentClipboardContents = clipboard.getContents(currentThread());
processContents(clipboard, currentClipboardContents);
regainOwnership(clipboard, currentClipboardContents);
Transferable currentClipboardContents = clipboard.getContents(null);
final MyClipboardContent<String>[] recentContent = new MyClipboardContent[]{new MyClipboardContent<>("")};
try {
recentContent[0].setContent(currentClipboardContents.getTransferData(DataFlavor.stringFlavor));
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
executor.scheduleAtFixedRate(() -> {
try {
Transferable newClipboard = clipboard.getContents(null);
String data = (String) newClipboard.getTransferData(DataFlavor.stringFlavor);
if (!data.equals(recentContent[0].getContent())) {
clipboardEvent.onCopy(new MyClipboardContent<>(data));
recentContent[0].setContent(data);
}
} catch (HeadlessException | IOException | UnsupportedFlavorException e) {
e.printStackTrace();
}
}, 0, 200, TimeUnit.MILLISECONDS);
}
public void setClipboardEvent(@NotNull ClipboardEvent clipboardEvent) {

@ -4,15 +4,24 @@ import me.goudham.ClipboardListener;
import me.goudham.listener.ClipboardEvent;
import me.goudham.model.MyClipboardContent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class ClipboardView implements ClipboardEvent {
private JPanel Clipboard;

Loading…
Cancel
Save