我将如何从系统剪贴板粘贴到java中的任意窗口?

发布于 2024-11-19 03:26:39 字数 233 浏览 6 评论 0原文

我正在制作一个聊天程序,它将粘贴用户输入的文本一定次数,这实际上会在受害者的屏幕上挤满一堆消息。我已经将文本复制到剪贴板上,但我无法弄清楚如何在不使用机器人类的情况下将其粘贴回来,我不想这样做,因为我无法弄清楚如何按命令键mac,所以它不会是多平台的,因为如果有人按下中间的一个键,它可能会做一些完全不同的事情。如果有人可以帮助我使用 Clipboard 类进行粘贴,那就太棒了。我已经在十亿个不同的网站上查找过这个问题,但无法弄清楚。提前致谢! :)

I am making a chat program that will paste the text the user puts in a certain number of times which essentially crowds the victims screen with a bunch of messages. I have already copied the text on the clipboard but I am not able to figure out how to paste it back without using the robot class which I don't want to do because I am not able to figure out how to press the command key for mac so it wouldn't be multi-platform and because if somebody presses a key in the middle it could do something completely different. If anybody can help me use the Clipboard class to paste that would be awesome. I've looked this up on like a billion different sites but can't figure it out. Thanks in advance! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

童话 2024-11-26 03:26:39

下面展示了如何将文本添加到剪贴板以及如何从剪贴板获取文本。

import java.awt.*;
import java.awt.datatransfer.*;
import java.io.*;

class ClipboardTest
{
    public static void main(String[] args)
        throws UnsupportedFlavorException, IOException
    {
        Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
        StringSelection testData;

        //  Add some test data

        if (args.length > 0)
            testData = new StringSelection( args[0] );
        else
            testData = new StringSelection( "Test Data" );

        c.setContents(testData, testData);

        //  Get clipboard contents, as a String

        Transferable t = c.getContents( null );

        if ( t.isDataFlavorSupported(DataFlavor.stringFlavor) )
        {
            Object o = t.getTransferData( DataFlavor.stringFlavor );
            String data = (String)t.getTransferData( DataFlavor.stringFlavor );
            System.out.println( "Clipboard contents: " + data );
        }

        System.exit(0);
    }
}

获得文本后,您可以通过执行以下操作将其添加到文本组件:

Document doc = textComponent.getDocument();
doc.insertString(....);

The following shows how to add text to the clipboard and how to get text from the clipboard.

import java.awt.*;
import java.awt.datatransfer.*;
import java.io.*;

class ClipboardTest
{
    public static void main(String[] args)
        throws UnsupportedFlavorException, IOException
    {
        Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
        StringSelection testData;

        //  Add some test data

        if (args.length > 0)
            testData = new StringSelection( args[0] );
        else
            testData = new StringSelection( "Test Data" );

        c.setContents(testData, testData);

        //  Get clipboard contents, as a String

        Transferable t = c.getContents( null );

        if ( t.isDataFlavorSupported(DataFlavor.stringFlavor) )
        {
            Object o = t.getTransferData( DataFlavor.stringFlavor );
            String data = (String)t.getTransferData( DataFlavor.stringFlavor );
            System.out.println( "Clipboard contents: " + data );
        }

        System.exit(0);
    }
}

Once you have the text you can add it to a text component by doing:

Document doc = textComponent.getDocument();
doc.insertString(....);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文