如何使我的代码输出为击键?

发布于 2024-11-05 22:18:03 字数 2039 浏览 1 评论 0原文

如何使我的代码输出为击键?我知道我必须使用 Robot 类,但是如何使其输出为对象?

import java.util.HashSet;
import java.util.Set;
import java.awt.Robot;
import java.awt.event.KeyEvent;

public class MainClass {

public static Set<String> generatePowerSet(String inputString)
{
    Set<String> result = new HashSet<String>();
    result.add(""); // empty string is an element of the power set

    if (inputString != null && !inputString.isEmpty())
    {
        int n = 1 << inputString.length(); //2^n - the number of elements in the power set 
        StringBuilder sb = new StringBuilder();

        for (int i = 1; i < n; i++) // skip empty set, i = 0
        {
            int k = i; // current number to manipulate
            sb.delete(0, sb.length()); // clear the StringBuilder
            for (int index = 0; index < inputString.length() && k > 0; index++) // break early when k == 0
            {
                if ((k & 1) == 1) // include char at index if bit at that index is 1
                {
                    sb.append(inputString.charAt(index));
                }
                k >>= 1;
            }
            result.add(sb.toString());
        }
    }
    return result;
}

public static void permuteString(String bs, String end) {
    if (end.length() <= 1)
        System.out.println(bs + end);//I THINK I HAVE TO CHANGE SOMETHING OVER HERE
    else
        for (int i = 0; i < end.length(); i++) {
            try {
                String newString = end.substring(0, i) + end.substring(i + 1);

                permuteString(bs + end.charAt(i), newString);
            } catch (StringIndexOutOfBoundsException exception) {
                exception.printStackTrace();
            }
        }
}

public static void main(String args[])  {

    Set<String> powerSet = generatePowerSet("String");

    Set<String> allPossibilities = new HashSet<String>();

    for(String s : powerSet)
    {
        permuteString(" ", s );
    }
}
}

How can I make I make my code output as keystrokes? I know I have to use the Robot class, but how can I make it output for an object?

import java.util.HashSet;
import java.util.Set;
import java.awt.Robot;
import java.awt.event.KeyEvent;

public class MainClass {

public static Set<String> generatePowerSet(String inputString)
{
    Set<String> result = new HashSet<String>();
    result.add(""); // empty string is an element of the power set

    if (inputString != null && !inputString.isEmpty())
    {
        int n = 1 << inputString.length(); //2^n - the number of elements in the power set 
        StringBuilder sb = new StringBuilder();

        for (int i = 1; i < n; i++) // skip empty set, i = 0
        {
            int k = i; // current number to manipulate
            sb.delete(0, sb.length()); // clear the StringBuilder
            for (int index = 0; index < inputString.length() && k > 0; index++) // break early when k == 0
            {
                if ((k & 1) == 1) // include char at index if bit at that index is 1
                {
                    sb.append(inputString.charAt(index));
                }
                k >>= 1;
            }
            result.add(sb.toString());
        }
    }
    return result;
}

public static void permuteString(String bs, String end) {
    if (end.length() <= 1)
        System.out.println(bs + end);//I THINK I HAVE TO CHANGE SOMETHING OVER HERE
    else
        for (int i = 0; i < end.length(); i++) {
            try {
                String newString = end.substring(0, i) + end.substring(i + 1);

                permuteString(bs + end.charAt(i), newString);
            } catch (StringIndexOutOfBoundsException exception) {
                exception.printStackTrace();
            }
        }
}

public static void main(String args[])  {

    Set<String> powerSet = generatePowerSet("String");

    Set<String> allPossibilities = new HashSet<String>();

    for(String s : powerSet)
    {
        permuteString(" ", s );
    }
}
}

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

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

发布评论

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

评论(1

梨涡少年 2024-11-12 22:18:03

这是一个使用 Robot示例

附录:

我知道了如何一次发送一个字符,但不发送字符串。

Robot 使用KeyEvent,并且没有简单的反向映射。相反,有一百万种方法可以推动大胜利;下面是一些示例:

  1. 使用 javax.swing.Timer 一次滚动文本一个字符,如图所示 此处

  2. 争夺“胜利!”图像,如此处所示。

  3. 淡化“胜利!”如此处所示。

附录:

有没有办法将我输出到控制台窗口的所有内容放入文件中?

java -cp build/classes MainClass > somefile.txt

Here's an example using Robot.

Addendum:

I got how to send one character out at a time but not Strings.

Robot uses KeyEvent, and there's no simple reverse mapping. Instead, there's a million ways to animate the big win; here's a few examples:

  1. Scroll the text one character at a time using javax.swing.Timer, as shown here.

  2. Scramble an "Win!" image, as shown here.

  3. Fade the "Win!" in, as shown here.

Addendum:

Is there a way to put everything I outputted into the console window into a file?

java -cp build/classes MainClass > somefile.txt
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文