机器人 - 按住左键单击时自动单击

Robot - AutoClicking while holding down left click

有没有办法让机器人在你按住左键点击然后松开时自动停止?这就是我目前拥有的...这不起作用。

public void nativeMousePressed(NativeMouseEvent e) {
    if (!disable) {
        if (e.getButton() == MouseEvent.BUTTON1) {
            if (!randomCPS) {
                robotClick(cps, typeOfClick);
            } else if (randomCPS) {
                robotRandomizedBetweenClicks(fromCPS, toCPS, typeOfClick);
            }
        }
    }
}


public void nativeMouseReleased(NativeMouseEvent e) {
    if (e.getButton() == MouseEvent.BUTTON1) {
        disable = true;
        running = false;
    }
}

您可以创建一个名为 clicking 的布尔值。在您的 mousePressed 方法中,将 clicking 设置为 true,在 mouseReleased 中将其设置为 false。

private boolean clicking = false;
public void nativeMousePressed(NativeMouseEvent e) {
   clicking = true;
      if (!disable) {
         if (e.getButton() == MouseEvent.BUTTON1) {
            if (!randomCPS) {
              robotClick(cps, typeOfClick);
              } else if (randomCPS) {
            robotRandomizedBetweenClicks(fromCPS, toCPS, typeOfClick);
            }
       }
    }
 }


public void nativeMouseReleased(NativeMouseEvent e) {
  clicking = false;
  if (e.getButton() == MouseEvent.BUTTON1) {
    disable = true;
    running = false;
}
}

这样,在您按下鼠标的过程中,您可以调用您想要使用的任何函数,并在您松开按钮时停止它。如果这意味着该函数被调用得太频繁,您可以实现一个计数器,每调用五次(或更多次)就激活该函数。

如果你想让它不断地 click/follow 一条路径,基本上就像在 MS Paint 中画一条线,然后考虑使用 'mouseDrag' 或 'MouseClick'.

让您的 JPanel 扩展 MouseMotionListener & MouseListener

@Override
public void mouseClicked(MouseEvent me) //try mouseDragged or the other methods which you're able to override once you've extended the classes I've mentioned above.
{       
    if (!randomCPS) 
    {
        robotClick(cps, typeOfClick);
    } 
    else if (randomCPS)
    {
        robotRandomizedBetweenClicks(fromCPS, toCPS, typeOfClick);
    }
}

@Override
public void mouseReleased(MouseEvent me) 
{
    disable = true;
    running = false;
}