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) {}
}