如何让机器人按住鼠标按钮一段时间?
我正在使用 Java 使用 Robot 类生成鼠标按下操作:
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
但是,我希望机器人按下按钮一段时间。我怎样才能实现这个目标?
I am using Java to generate a mouse press using the Robot class:
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
However, I want the Robot to press the button for a certain period of time. How can I achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需在两个操作之间休眠一下(以毫秒为单位):
Thread.sleep(long millis);
Robot.delay(long millis);
>Just sleep a bit between the two actions (specified in milliseconds):
Thread.sleep(long millis);
Robot.delay(long millis);
我这样做了,很简单:当您检测到按下鼠标时,您保存
System.currentTimeMillis()
。当您检测到鼠标被释放时,您只需检查它被按下的时间。如果您希望在一定时间后执行操作,即使鼠标仍然被按下,您也可以启动一个线程,该线程在按下时存活所需的时间,并在释放时中断它。如果线程在您想要的时间内没有被中断,则将执行该操作。
I did that, it's simple: when you detect the mouse is pressed, you save the
System.currentTimeMillis()
. When you detect the mouse is released, you just check how long it was pressed.If you want the action to be made after a certain amount of time, even if the mouse is still pressed, you start a thread that lives the desired amount of time when pressed and you interrupt it when releasing. If the thread isn't interrupted in the amount of time you wanted, the action will be performed.