0

我想在 windows 中获取 crtl+c 或右键单击复制之类的事件,这可以对运行的 java 应用程序执行该事件,

这意味着如果有人复制了一些文本,则应该将其粘贴到 java 应用程序文本区域中......

我已经制作了 java 应用程序,它可以通过 main 方法接受参数。

但是如何触发从windows到java的事件..

4

1 回答 1

2

最简单的方法是监视更改Toolkit.getSystemClipboard

有两种方法可以做到这一点。您可以监控对DataFlavour.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
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.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ClipboardMonitor {

    public static void main(String[] args) {
        new ClipboardMonitor();
    }

    public ClipboardMonitor() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JTextArea textArea;

        public TestPane() {
            textArea = new JTextArea(10, 10);
            setLayout(new BorderLayout());
            add(new JScrollPane(textArea));
            Toolkit.getDefaultToolkit().getSystemClipboard().addFlavorListener(new FlavorListener() {
                @Override
                public void flavorsChanged(FlavorEvent e) {
                    setText(getClipboardContents());
                }
            });
            Thread t = new Thread(new ContentsMonitor());
            t.setDaemon(true);
            t.start();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        protected String getClipboardContents() {
            String text = null;
            Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
            if (clipboard.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
                try {
                    Transferable contents = clipboard.getContents(TestPane.this);
                    text = (String) contents.getTransferData(DataFlavor.stringFlavor);
                } catch (UnsupportedFlavorException | IOException ex) {
                    ex.printStackTrace();
                }
            }
            return text;
        }

        protected void setText(final String text) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    textArea.setText(text);
                }
            });
        }

        public class ContentsMonitor implements Runnable {

            @Override
            public void run() {
                String previous = getClipboardContents();
                while (true) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException ex) {
                    }
                    String text = getClipboardContents();
                    if (text != null && !text.equals(previous)) {
                        setText(text);
                        previous = text;
                    }
                }
            }
        }
    }
}
于 2013-04-22T06:06:50.297 回答