0

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 );
    }
}
}
Jason Plank
  • 2,336
  • 5
  • 31
  • 40
  • What do you mean "output for an *object*"? – Hovercraft Full Of Eels May 07 '11 at 00:58
  • when i try to output it with a robot it says it needs a string not an object. –  May 07 '11 at 01:03
  • @TheWaller: that doesn't answer my question, and you are still not telling us anything at all about what you are ultimately trying to achieve. Can you provide more details for your question? Assume that we can't read your mind. – Hovercraft Full Of Eels May 07 '11 at 01:04
  • 1
    sorry. i am making a program that will output all possible combinations of a string. i have got that. now i have to get the Robot class to output them as keystrokes. How do I do that? –  May 07 '11 at 01:08
  • @TheWaller: I'm sorry, perhaps it's me, but I still don't know what you mean by "output them as keystrokes"? As keystrokes to what? As an ArrayList? If you don't get an answer soon, please clarify. – Hovercraft Full Of Eels May 07 '11 at 01:10
  • You should cite your [previous question](http://stackoverflow.com/questions/5905189/combinations-of-a-string) on this topic. – trashgod May 07 '11 at 01:12
  • http://download.oracle.com/javase/1.3/docs/api/java/awt/Robot.html that class has something called keypressed i want all keys for the string that is sent out to that and be put in to keystrokes. so if the program outputted win. it would type w i n –  May 07 '11 at 01:12
  • I hope that clarified that. If you have any more questions, I will try to answer them the best I can. –  May 07 '11 at 01:26

1 Answers1

1

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
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I got how to send one character out at a time but not Strings. –  May 07 '11 at 01:16
  • `Robot` uses `KeyEvent`. Instead, add the text to a label one character at a time using `javax.swing.Timer`. – trashgod May 07 '11 at 01:27
  • Is there a way to put everything I outputted into the console window into a file? –  May 07 '11 at 01:52
  • I mean in the source code. Then i want to make another program that will just read the letters in it and keystroke them all out. –  May 07 '11 at 02:08
  • To a file? Try [this](http://stackoverflow.com/questions/tagged/java+text+file+save). – trashgod May 07 '11 at 02:43