0

我对 JNativeHook 库和 Java Robot 的操作有疑问。当我按 F9 时我的机器人应该运行,当我按 F6 时它应该停止,但它不工作。我不知道我在这里做错了什么。每次我试图停止机器人时,它都不会停止。代码说,当我按 F6 时,工作值应更改为 false,while 循环应停止调用机器人。

import com.github.kwhat.jnativehook.GlobalScreen;
import com.github.kwhat.jnativehook.NativeHookException;
import com.github.kwhat.jnativehook.keyboard.NativeKeyEvent;
import com.github.kwhat.jnativehook.keyboard.NativeKeyListener;
import javax.swing.*;
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Main extends JFrame implements NativeKeyListener {

    static int szerokosc = 2;
    static int dlugosc = 10;
    static boolean work = false;
    static Robot robot;
    static JTextField t1;
    static JTextField t3;
    static JTextField p1;
    JFrame jf;
    JLabel label, label2, label4;

    public Main() {
        try {
            robot = new Robot();
            jf = new JFrame();
            ImageIcon img = new ImageIcon("pickaxe.png");
            JTextPane tp = new JTextPane();
            jf.setLocation(800, 300);
            tp.setFont(new Font("Arial", 0, 32));
            jf.setTitle("Kopacz by Macijke");
            jf.setIconImage(img.getImage());
            jf.setPreferredSize(new Dimension(350, 350));
            jf.setLayout(new FlowLayout());
            label = new JLabel("Kliknij F9, aby włączyć kopanie na 6/3/3!");
            label.setBounds(50, 10, 250, 20);
            jf.add(label);

            label2 = new JLabel("Komenda nr 1");
            label2.setBounds(10, 20, 250, 90);
            t1 = new JTextField("/repair");
            t1.setBounds(100, 50, 150, 30);
            label2.setVisible(true);

            label4 = new JLabel("Długość");
            label4.setBounds(10, 240, 250, 90);
            t3 = new JTextField("10");
            t3.setBounds(65, 270, 50, 30);

            // ============
            p1 = new JTextField("11");
            p1.setBounds(255, 50, 30, 30);

            // ============

            jf.setDefaultCloseOperation(3);
            jf.setLayout(null);
            jf.add(t1);
            jf.add(t3);
            jf.add(label2);
            jf.add(label4);
            jf.add(p1);
            jf.pack();
            jf.setVisible(true);
        } catch (AWTException var3) {
            var3.printStackTrace();
        }
    }


    public static void main(String[] args) {
        try {
            GlobalScreen.registerNativeHook();
            Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName());
            logger.setLevel(Level.ALL);
            logger.setUseParentHandlers(false);
        } catch (NativeHookException e) {
            e.printStackTrace();
        }

        GlobalScreen.addNativeKeyListener(new Main());
    }

    @Override
    public void nativeKeyPressed(NativeKeyEvent e) {
        //
    }

    @Override
    public void nativeKeyReleased(NativeKeyEvent e) {
        if (e.getKeyCode() == NativeKeyEvent.VC_F6) {
            work = false;
        }

        if (e.getKeyCode() == NativeKeyEvent.VC_F9) {
            dlugosc = Integer.parseInt(t3.getText());
            work = true;
            int i = 1;
            while (work) {
                robot.mousePress(InputEvent.BUTTON1_MASK);
                for (int z = 0; z < dlugosc; z++) {
                    robot.keyPress(KeyEvent.VK_W);
                    robot.delay(210);
                    robot.keyRelease(KeyEvent.VK_W);

                }

                for (int a = 0; a < szerokosc; a++) {
                    robot.keyPress(KeyEvent.VK_A);
                    robot.delay(150);
                    robot.keyRelease(KeyEvent.VK_A);
                }

                for (int z = 0; z < dlugosc; z++) {
                    robot.keyPress(KeyEvent.VK_S);
                    robot.delay(210);
                    robot.keyRelease(KeyEvent.VK_S);

                }

                for (int z = 0; z < szerokosc; z++) {
                    robot.keyPress(KeyEvent.VK_D);
                    robot.delay(150);
                    robot.keyRelease(KeyEvent.VK_D);
                }
                robot.mouseRelease(InputEvent.BUTTON1_MASK);

                if (i % 25 == 0) {
                    robot.delay(100);
                    robot.keyPress(KeyEvent.VK_2);
                    robot.keyRelease(KeyEvent.VK_2);
                    robot.delay(250);
                    robot.mousePress(InputEvent.BUTTON3_MASK);
                    robot.delay(4000);
                    robot.mouseRelease(InputEvent.BUTTON3_MASK);
                    robot.delay(250);
                    robot.keyPress(KeyEvent.VK_1);
                    robot.keyRelease(KeyEvent.VK_1);
                    robot.delay(100);
                }
                i++;
            }
        }


    }

    @Override
    public void nativeKeyTyped(NativeKeyEvent e) {
        //
    }

}
4

0 回答 0