【问题标题】:Robot - AutoClicking while holding down left click机器人 - 按住左键单击时自动单击
【发布时间】:2016-05-09 19:01:31
【问题描述】:

如果您按住左键然后释放时停止,有没有办法让机器人自动点击?这是我目前所拥有的......这是行不通的。

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;
    }
}

【问题讨论】:

  • 你能展示你的main方法吗?

标签: java mouseevent awtrobot


【解决方案1】:

您可以创建一个名为 click 的布尔值。在 mousePressed 方法中,将 click 设置为 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;
}
}

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

【讨论】:

  • 得到它的工作,不得不玩了一下,但得到了它的工作。也很抱歉回复晚了。谢谢!
【解决方案2】:

如果您希望它不断单击/跟随路径,例如本质上就像在 MS Paint 中绘制一条线,那么请考虑使用“mouseDrag”或“MouseClick”。

让您的 JPanel 扩展 MouseMotionListenerMouseListener

@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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-28
    • 1970-01-01
    • 2017-07-29
    • 2011-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-08
    相关资源
    最近更新 更多