有没有一种方法可以在Java中锁定鼠标在一个位置一段时间?
我尝试过以下代码:
while(timer == true){
Robot bot = new Robot();
bot.mouseMove(x, y);
}
但是当用户移动鼠标时,它会不愉快地来回跳动(从用户拖动的位置到应该锁定的位置)。
有没有更好的方法来解决这个问题?或者我可以完全禁用鼠标的用户输入吗?谢谢!
有没有一种方法可以在Java中锁定鼠标在一个位置一段时间?
我尝试过以下代码:
while(timer == true){
Robot bot = new Robot();
bot.mouseMove(x, y);
}
public class Stop extends JFrame {
static Robot robot = null;
static Rectangle bounds = new Rectangle(300, 300, 300, 300);
static int lastX = 450; static int lastY = 450;
Stop() {
try {
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
addMouseMotionListener(new MouseStop());
getContentPane().add(new JLabel("<html>A sticky situation<br>Hold SHIFT to get out of it", JLabel.CENTER));
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBounds(bounds);
setVisible(true);
}
private static class MouseStop extends MouseAdapter {
@Override
public void mouseMoved(MouseEvent e) {
if(e.isShiftDown()) {
lastX = e.getXOnScreen();
lastY = e.getYOnScreen();
}
else
robot.mouseMove(lastX, lastY);
}
}
public static void main(String args[]) {
new Stop();
}
}
编辑:我刚有一个想法,就是将光标的绘制处理成看起来像鼠标没有移动。如果有什么实现的代码,我会添加上去。