In Java, there is one interesting class that provides control of the mouse cursor and keyboard. This is a Robot class.
Can you please tell how you can interrupt the work of the cycle in which the robot is working. For example, by pressing a button on the keyboard.
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.InputEvent; import java.awt.event.KeyEvent; import javax.swing.*; class myFrame extends JFrame { private Robot robot; private JButton bt1; public myFrame() { setTitle("Robot"); JPanel panel = new JPanel(); GraphicsEnvironment env = GraphicsEnvironment .getLocalGraphicsEnvironment(); GraphicsDevice screen = env.getDefaultScreenDevice(); try { robot = new Robot(screen); } catch (AWTException ex) { } bt1 = new JButton("Start"); panel.add(bt1); bt1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { for (int i = 0; i < 100; i++) // Π·Π΄Π΅ΡΡ ΠΈ ΠΏΡΠΎΠΈΡΡ
ΠΎΠ΄ΡΡ Π²ΡΠ΅ Π΄Π΅ΠΉΡΡΠ²ΠΈΠ΅ // Ρ Π½Π°ΡΠΈΠΌ ΡΠΎΠ±ΠΎΡΠΎΠΌ { robot.mouseMove(500, 500 + 15 * i); // Π΄Π²ΠΈΠ³Π°Π΅ΠΌ ΠΌΡΡΠΊΡ Π½Π° robot.delay(300); // Π·Π°Π΄Π°Π½Π½ΡΡ ΠΊΠΎΠΎΡΠ΄ΠΈΠ½Π°ΡΡ robot.mousePress(InputEvent.BUTTON1_MASK); // Π½Π°ΠΆΠΈΠΌΠ°Π΅ΠΌ Π»Π΅Π²ΡΡ // ΠΊΠ½ΠΎΠΏΠΊΡ ΠΌΡΡΠΈ robot.delay(300); // 300 ΠΌΠΈΠ»Π»ΠΈΡΠ΅ΠΊΡΠ½Π΄Π½Π°Ρ ΠΏΠ°ΡΠ·Π° robot.mouseRelease(InputEvent.BUTTON1_MASK); // ΠΎΡΠΆΠΈΠΌΠ°Π΅ΠΌ // Π»Π΅Π²ΡΡ // ΠΊΠ½ΠΎΠΏΠΊΡ // ΠΌΡΡΠΈ robot.delay(300); robot.keyPress('O'); // Π½Π°ΠΆΠΈΠΌΠ°Π΅ΠΌ ΠΊΠ½ΠΎΠΏΡ 'O' Ρ ΠΊΠ»Π°Π²ΠΈΠ°ΡΡΡΡ robot.keyRelease('O'); // ΠΎΡΠΆΠΈΠΌΠ°Π΅ΠΌ ΠΊΠ½ΠΎΠΏΡ 'O' Ρ ΠΊΠ»Π°Π²ΠΈΠ°ΡΡΡΡ robot.delay(300); robot.keyPress(KeyEvent.VK_DOWN); // Π½Π°ΠΆΠΈΠΌΠ°Π΅ΠΌ ΠΊΠ½ΠΎΠΏΡ 'DOWN' Ρ ΠΊΠ»Π°Π²ΠΈΠ°ΡΡΡΡ robot.keyPress(KeyEvent.VK_DOWN); robot.keyPress(KeyEvent.VK_DOWN); } } }); Container pane = getContentPane(); pane.add(panel); pack(); } } public class MyRobot { public static void main(String[] args) { myFrame frame = new myFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
break
after theInterupted
flag has been set on theInterupted
(which needs to be checked). - delphist007