我正在为DS模拟器编写一个口袋妖怪教练。我希望程序做的是在运行机器人的时候,花点时间听一下可能暂停训练器的按键,如果没有按下任何东西,那就继续吧。
所以我所做的是在机器人按左/右键完成后实现一个关键事件,但它似乎没有检测到按键。
计划代码:
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());
}
};
}
}
然后我尝试为KeyListener
创建一个新类,并在结束Robot KeyEvent之后添加new KeyPressed();
来调用该侦听器,但它没有响应。
添加了new KeyPressed();
:
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();
}
新KeyPressed
KeyListener
课程:
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) {}
}
到目前为止,没有一个有效,但我认为它可能是我的位置,或者我没有给自己足够的时间来按键,因为它唯一的延迟是275毫秒。
更新:收听任何按键:
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) {}
}