gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 15:51:33 25 4
gpt4 key购买 nike

如何才能将代码输出为击键?我知道我必须使用 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 );
}
}
}

最佳答案

这是一个example使用机器人

附录:

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

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

  1. 使用 javax.swing.Timer 将文本一次滚动一个字符,如图 here .

  2. 争夺“胜利!”图像,如图here .

  3. 淡化“胜利!”中,如图here .

附录:

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

java -cp build/classes MainClass > somefile.txt

关于java - 如何使我的代码输出为击键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5918435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com