[英]Java Robot doesnt Press Enter Key how it should
Im trying to program a litte Robot, that should just write for some hours the same phrase with a delay.我正在尝试编写一个小型机器人,它应该只写几个小时相同的短语,但会有延迟。
But somehow, if i have mor than 1 Letter at the same time before the Enter Key, it rather types a Pyramide.但不知何故,如果我在 Enter 键之前同时有超过 1 个字母,它宁愿键入一个 Pyramide。
For Example, if i wanna print "ted", it prints the following:例如,如果我想打印“ted”,它会打印以下内容:
ted泰德
tedted泰特
tedtedted泰泰泰
tedtedtedted特特特特特
[...] [...]
(There is no empty line between the Pyramide-Lines) (金字塔线之间没有空线)
It gets really frustrating.这真的令人沮丧。
I Tried many solutions, but none worked.我尝试了许多解决方案,但都没有奏效。 Making a Delay for the robot, an extra Robot for the Enter Key, put it in an extra Thread or creating new Robots every time before a new Typing.
为机器人做一个延迟,一个额外的机器人输入键,把它放在一个额外的线程中,或者每次在新的打字之前创建新的机器人。 It just doesnt work.
它只是行不通。 What am i doing wrong?
我究竟做错了什么? Here is a SSCCE with some trys i did:
这是一个 SSCCE,我做了一些尝试:
@SuppressWarnings("CallToPrintStackTrace")
public class RobotTest {
private static Robot robo;
private static Robot okRobo;
static {
createRobos();
}
private static void createRobos(){
try {
robo = new Robot();
okRobo = new Robot();
} catch (AWTException ex) {
ex.printStackTrace();
}
}
@SuppressWarnings({"CallToPrintStackTrace", "SleepWhileInLoop"})
public static void main(String[] args) throws InterruptedException {
Thread.sleep(5000);
for (int i = 0; i < 16; i++) {
createRobos();
perform();
// Thread.sleep(500);
performOk();
}
}
private static void perform() {
robo.keyPress(KeyEvent.VK_SHIFT);
robo.keyPress(KeyEvent.VK_1);
robo.keyRelease(KeyEvent.VK_SHIFT);
robo.keyRelease(KeyEvent.VK_1);
robo.keyPress(KeyEvent.VK_B);
robo.keyRelease(KeyEvent.VK_B);
robo.keyPress(KeyEvent.VK_O);
robo.keyRelease(KeyEvent.VK_O);
robo.keyPress(KeyEvent.VK_O);
robo.keyRelease(KeyEvent.VK_O);
robo.keyPress(KeyEvent.VK_S);
robo.keyRelease(KeyEvent.VK_S);
robo.keyPress(KeyEvent.VK_T);
robo.keyRelease(KeyEvent.VK_T);
robo.keyPress(KeyEvent.VK_E);
robo.keyRelease(KeyEvent.VK_E);
robo.keyPress(KeyEvent.VK_D);
robo.keyRelease(KeyEvent.VK_D);
// robo.waitForIdle();
}
private static void performOk() {
okRobo.keyPress(KeyEvent.VK_ENTER);
okRobo.keyRelease(KeyEvent.VK_ENTER);
// okRobo.waitForIdle();
}
} }
And here is my first try, that should work in my opinion too, but it doesnt:这是我的第一次尝试,我认为这也应该有效,但它没有:
public class RobotTest {
private static Robot robo;
@SuppressWarnings("CallToPrintStackTrace")
static {
try {
robo = new Robot();
} catch (AWTException ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) throws InterruptedException {
Thread.sleep(5000);
for (int i = 0; i < 16; i++) {
perform();
}
}
private static void perform() {
robo.keyPress(KeyEvent.VK_SHIFT);
robo.keyPress(KeyEvent.VK_1);
robo.keyRelease(KeyEvent.VK_SHIFT);
robo.keyRelease(KeyEvent.VK_1);
robo.keyPress(KeyEvent.VK_B);
robo.keyRelease(KeyEvent.VK_B);
robo.keyPress(KeyEvent.VK_O);
robo.keyRelease(KeyEvent.VK_O);
robo.keyPress(KeyEvent.VK_O);
robo.keyRelease(KeyEvent.VK_O);
robo.keyPress(KeyEvent.VK_S);
robo.keyRelease(KeyEvent.VK_S);
robo.keyPress(KeyEvent.VK_T);
robo.keyRelease(KeyEvent.VK_T);
robo.keyPress(KeyEvent.VK_E);
robo.keyRelease(KeyEvent.VK_E);
robo.keyPress(KeyEvent.VK_D);
robo.keyRelease(KeyEvent.VK_D);
robo.keyPress(KeyEvent.VK_ENTER);
robo.keyRelease(KeyEvent.VK_ENTER);
robo.delay(500);
}
createRobos()
outside of the loop.createRobos()
。 That is unless you have a specific reason that you're doing it that way.Thread.sleep()
, you can use the delay()
method within the Robot
class.Thread.sleep()
,您可以使用Robot
class 中的delay()
方法。 This is if you want to add a delay between Robot
method calls.Robot
方法调用之间添加延迟。 You may want to try to add a delay between when you're typing out the letter keys and when you're pressing the enter key and after you press the enter key.您可能想尝试在输入字母键和按下回车键之间以及按下回车键之后添加延迟。 A 50 - 100 ms delay will usually do the trick.
50 - 100 毫秒的延迟通常可以解决问题。 Sometimes, things get a little messed up, especially when you throw
Thread.sleep()
into the mix.有时,事情会变得有些混乱,尤其是当您将
Thread.sleep()
投入其中时。
I ran your code with these small changes and it seemed to work fine.我用这些小改动运行了你的代码,它似乎工作正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.