我正在开发一个程序,它以类似人类的方式在 Java 中模拟按键。目标是每 x 秒发送一次随机按键(x 是两个整数之间的随机数)。这是我到目前为止的代码:
public class AutoKeyboard {
public static int randInt(int min, int max) { // Method to generate random int
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
public static void main(String[] args) throws InterruptedException {
int running = 1;
while (running == 1) {
try {
int delay = randInt(336415,783410); // Generates random int between two integers
Robot robot = new Robot();
Thread.sleep(delay); // Thread sleeps for x (random int) milliseconds
robot.keyPress(KeyEvent.VK_SPACE); // Simulating press of space bar
} catch (AWTException e) {
e.printStackTrace();
}
}
}
}
我想要实现的是 Keyevent.VK_SPACE 是随机的,因此它可以是列表中的任何键(例如它会按 AD 中的随机键),而不是空格键。我该怎么做呢?我想不出具有我已经拥有的编程知识的合乎逻辑的解决方案(可悲的是很少)
感谢您的任何回复。