0

I'm writing a pokemon trainer for a DS emulator. What I want the program to do is while its running the Robot, take a time and listen for any key presses that may pause the trainer and if nothing is pressed, then keep going.

So what I did was to implement a key event after the Robot gets done pressing the left / right key, but it doesn't seem to detect the key press.

Program Code:

package frame;

public class start {
    //Global Variables
    static int time;
    static int waitTime;
    public static void main (String[] args) throws InterruptedException, AWTException, IOException {
        trainerMenu();
    }

    public static void trainerMenu() throws InterruptedException, AWTException, IOException {
        Scanner timeToTrain = new Scanner(System.in);
        Scanner wait = new Scanner(System.in);      
        System.out.println("Welcome to xR34P3Rx's PokeMon Trainer!");
        System.out.print("Enter rounds to train your pokemon, (Each round takes aprox. 47 sec. ): ");
        time = timeToTrain.nextInt();
        System.out.print("How much time do you need to get to your emulator: ");
        waitTime = wait.nextInt();
        goTrain();
    }

    public static void goTrain() throws InterruptedException, AWTException, IOException {
        while (time != 0) {
            walkRight();
            walkLeft();
            time--;
        }
        System.out.println("Training Complete!\nPress any key to return to the menu...");
        System.in.read();
        trainerMenu();
    }

    public static void walkRight() throws InterruptedException, AWTException {
        Thread.sleep(waitTime);
        int steps = 79;

        //Robot 1
        try {
            while (steps!=0) {
                Robot robot = new Robot();

                robot.keyPress(KeyEvent.VK_RIGHT);
                steps--;
                Thread.sleep(275);
            }
        } finally {}
        Robot end = new Robot();
        end.keyRelease(KeyEvent.VK_RIGHT);

        //Pause
        new KeyPressed() {
            public void keyTyped(KeyEvent e) {}         
            public void KeyPressed(KeyEvent e) {
                System.out.println("Key pressed code=" + e.getKeyCode() + ", char=" + e.getKeyChar());
            }
        };
    }

    public static void walkLeft() throws InterruptedException, AWTException {
        Thread.sleep(waitTime);
        int steps = 79;

        //Robot 1
        try {
            while (steps!=0) {
                Robot robot = new Robot();

                robot.keyPress(KeyEvent.VK_LEFT);
                steps--;
                Thread.sleep(275);
            }
        } finally {}
        Robot end = new Robot();
        end.keyRelease(KeyEvent.VK_LEFT);

        new KeyPressed() {
            public void keyTyped(KeyEvent e) {}

            public void KeyPressed(KeyEvent e) {
                System.out.println("Key pressed code=" + e.getKeyCode() + ", char=" + e.getKeyChar());
            }
        };
    }
}

Then I tried to make a new class for the KeyListener and added new KeyPressed(); after the end Robot KeyEvent to call the listener, but it doesn't respond.

With new KeyPressed(); added:

public static void walkLeft() throws InterruptedException, AWTException {
    Thread.sleep(waitTime);
    int steps = 79;

    //Robot 1
    try {
        while (steps != 0) {
            Robot robot = new Robot();

            robot.keyPress(KeyEvent.VK_LEFT);
            steps--;
            Thread.sleep(275);
        }
    }finally {}
    Robot end = new Robot();
    end.keyRelease(KeyEvent.VK_LEFT);

    //Pause
    new KeyPressed();
}

New KeyPressed KeyListener class:

package frame;

public class KeyPressed implements KeyListener{

    @Override
    public void keyPressed(KeyEvent arg0) {
        // TODO Auto-generated method stub
        arg0.getKeyChar();
        if (arg0.getKeyChar() == 'z') {
            System.out.println("You pressed 'Z'");
        }
    }

    @Override
    public void keyReleased(KeyEvent arg0) {}
    @Override
    public void keyTyped(KeyEvent arg0) {}

}

So far, none have worked, but I think it could be my placement or I'm not giving myself enough time to press a key, since the only delay it has is 275 milliseconds.

Updated: Listen for any key presses:

package frame;

public class KeyPressed implements KeyListener{
    @Override
    public void keyPressed(KeyEvent arg0) {
        char pressed = arg0.getKeyChar();
        System.out.println("You pressed" + pressed);
    }

    @Override
    public void keyReleased(KeyEvent arg0) {}
    @Override
    public void keyTyped(KeyEvent arg0) {}

}
Unihedron
  • 10,902
  • 13
  • 62
  • 72
xR34P3Rx
  • 395
  • 9
  • 28
  • I now tried to change the KeyListener class to listen for _any_ keys pressed and still didnt work. – xR34P3Rx Nov 10 '14 at 10:42
  • 1
    When you say "none have worked" you mean that it doesn't produce the desired result or result in some kind of error? – Braiam Nov 10 '14 at 11:56
  • doesn't produce the desired result, i was told earlier in the chat rooms, that i need to look into stdin – xR34P3Rx Nov 10 '14 at 12:05
  • It doesn't listen because you put your thread to sleep while the robot runs, use a while loop to do nothing and wait instead of using sleep http://stackoverflow.com/questions/9143719/java-alternative-to-thread-sleep – Infinite Recursion Nov 10 '14 at 12:12
  • Replace the Thread.sleeps in your walkLeft and walkRight, it will work. Sleeping thread can't listen to key inputs. Or you can use TimerTask and put the robot actions on a TimerTask – Infinite Recursion Nov 10 '14 at 12:13
  • hmm, that makes sense. should i try TimeUnit.SECONDS.wait();? – xR34P3Rx Nov 10 '14 at 12:17
  • ok, i think im getting it now, anything that will make the program pause/sleep, wont work? – xR34P3Rx Nov 10 '14 at 13:08
  • Yes, when execution thread sleeps or is paused, it doesn't do anything, like receiving inputs events etc – Infinite Recursion Nov 11 '14 at 04:02
  • alright then, im doing some tests to learn how to use the Timer api. we'll see how it goes – xR34P3Rx Nov 11 '14 at 08:38

0 Answers0