简体   繁体   中英

Exit a while loop using KeyListener

I'm trying to make my own Autoclicker and can't figure out how to exit the "clicking" using a KeyListener. I get that a part of the problem is that the window needs to be in focus for a KeyListener to work, but I've got no clue on how to implement that.

I'll add all the code so you can see for yourself on how to solve it.

public class GUIMain extends JFrame {
protected static GUIMain window = null;
protected static Box mainFrame = new Box(BoxLayout.Y_AXIS);
protected Click clicker = null;

public GUIMain(String title) {
    super(title);
    mainBox();
    startRunning();

    this.addKeyListener(clicker);
    this.toFront();
    this.pack();
    this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    this.setVisible(true);
}

public static void main(String[] args) {
    window = new GUIMain("AutoClicker v0.1");
}

protected void mainBox() {
    this.setLayout(new BoxLayout(this.getContentPane(), 
            BoxLayout.Y_AXIS));
    mainFrame.setPreferredSize(new Dimension(250, 150));
    this.getContentPane().setBackground(Color.WHITE);
    this.add(mainFrame);
    mainFrame.add(Box.createVerticalGlue());
}

protected void startRunning() {
    JButton startButton = new JButton(" Start ");
    mainFrame.add(startButton);
    mainFrame.add(Box.createVerticalGlue());
    startButton.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent e) {
            clicker = new Click();
        }
    });

}
}

and now the "Click" class that implements the KeyListener. I've got the i<100 part in the while loop just so that it doesn't loop forever while fixing it.

public class Click extends JComponent implements KeyListener{
private static boolean runClicks = true;
static Robot robot = null;

public Click() {
    mouseClick();
}

protected static void mouseClick() {
    int i = 0;
    try {
        robot = new Robot ();
    } catch (AWTException e) {
        e.printStackTrace();
    }
    while(runClicks && i < 100) {
            click(-1440, 540);
            i++;
    }
}

private static void click(int x, int y) {
    robot.mouseMove(x, y);
    robot.delay(50);
    robot.mousePress(MouseEvent.BUTTON1_DOWN_MASK);
    robot.mouseRelease(MouseEvent.BUTTON1_DOWN_MASK);
}

@Override
public void keyPressed(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_F9) {
        runClicks = false;
    }
}

@Override
public void keyReleased(KeyEvent e) {
}

@Override
public void keyTyped(KeyEvent e) {  
}
}

Do I need to think of a new way to solve this or can it be done with some modifications?

I am not a Java expert by any means, but if you need to give the window focus, you can use the requestFocus() call.

public static void main(string[] args) {
    JFrame frame = new JFrame();
    frame.add // whatever you want to add to your frame and sizing
    frame.requestFocus();
    frame.pack();
    frame.setVisible(true);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM