Answer the question
In order to leave comments, you need to log in
Java. Breaking out of an endless loop?
Hello ladies and gentlemen!
Help, please, to solve the problem - it is necessary to exit the infinite loop by pressing a key (eg ESC).
Here is my code:
package likearobot;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.event.*;
public class LikeARobot {
public static void main(String[] args) {
KeyEvent e;
Robot bot = null;
try {
bot = new Robot();
} catch (Exception failed) {
System.err.println("Failed instantiating Robot: " + failed);
}
int mask = InputEvent.BUTTON1_DOWN_MASK;
//Открываем окно браузера
bot.delay(1000);
bot.mouseMove(140, 1190);
bot.mousePress(InputEvent.BUTTON1_MASK);
bot.mouseRelease(InputEvent.BUTTON1_MASK);
bot.delay(2000);
for ( ; ; )
{
int picture = 300;
int pictureTwo = 370;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 12; j++)
{
//Открываем 1-ю картинку
bot.mouseMove(picture, pictureTwo);
picture = picture + 130;
bot.mousePress(InputEvent.BUTTON1_MASK);
bot.mouseRelease(InputEvent.BUTTON1_MASK);
bot.delay(2000);
//Разворачиваем окно
bot.mouseMove(1360, 309);
bot.mousePress(InputEvent.BUTTON1_MASK);
bot.mouseRelease(InputEvent.BUTTON1_MASK);
bot.delay(2000);
//Cтавим лайк
bot.mouseMove(1130, 863);
bot.mousePress(InputEvent.BUTTON1_MASK);
bot.mouseRelease(InputEvent.BUTTON1_MASK);
bot.delay(100);
bot.mouseMove(1128, 815);
bot.mousePress(InputEvent.BUTTON1_MASK);
bot.mouseRelease(InputEvent.BUTTON1_MASK);
bot.mouseMove(1168, 867);
bot.mousePress(InputEvent.BUTTON1_MASK);
bot.mouseRelease(InputEvent.BUTTON1_MASK);
bot.mouseMove(1072, 867);
bot.mousePress(InputEvent.BUTTON1_MASK);
bot.mouseRelease(InputEvent.BUTTON1_MASK);
bot.mouseMove(1128, 736);
bot.mousePress(InputEvent.BUTTON1_MASK);
bot.mouseRelease(InputEvent.BUTTON1_MASK);
//Закрываем окно
bot.delay(2000);
bot.mouseMove(1895, 7);
bot.mousePress(InputEvent.BUTTON1_MASK);
bot.mouseRelease(InputEvent.BUTTON1_MASK);
bot.delay(2000);
}
picture = 300;
bot.delay(2000);
bot.keyPress(KeyEvent.VK_F5);
bot.delay(2000);
}
bot.delay(2000);
bot.keyPress(KeyEvent.VK_F5);
bot.delay(2000);
}
}
}
public void keyPressed(KeyEvent e) {}
public void keyReleased(KeyEvent e) {
if (e.getKeyChar() == KeyEvent.VK_ENTER) {
System.exit(0);
}
}
public void keyTyped(KeyEvent e) {}
Answer the question
In order to leave comments, you need to log in
Create a Boolean variable and then look at my code snippet and make it look like it.
Oh yes, it's in JavaFX, but rewriting for swing is not a problem
public void handle(KeyEvent ke) {
if (ke.getCode().getName().equals("F12")) {
toExit = false;
Thread clicking = new Thread(new Runnable() {
@Override
public void run() {
while (!toExit) {
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(delayVal);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(delayVal);
}
}
});
clicking.start();
} else if (ke.getCode().getName().equals("F11")) {
toExit = true;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question