[英]How can I make I make my code output as keystrokes?
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?
我知道我必须使用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 );
}
}
}
Here's an example using Robot
. 这是使用
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. Robot
使用KeyEvent
,并且没有简单的反向映射。 Instead, there's a million ways to animate the big win; 取而代之的是,有一百万种方法来动画大胜利。 here's a few examples:
这里有几个例子:
Scroll the text one character at a time using javax.swing.Timer
, as shown here . 滚动文字的一个字符,在使用时间
javax.swing.Timer
,如图所示这里 。
Addendum: 附录:
Is there a way to put everything I outputted into the console window into a file?
有没有办法将我输出到控制台窗口的所有内容都放入一个文件中?
java -cp build/classes MainClass > somefile.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.