有没有办法在Java中将鼠标锁定在一个位置一段时间?
我试过这个:
while(timer == true){
Robot bot = new Robot();
bot.mouseMove(x, y);
}
但是,当用户移动鼠标时,它会令人不愉快地来回跳跃(从用户拖动的位置到应该锁定的位置)。
如果有更好的方法来做到这一点,有什么想法吗?或者我可以完全禁用鼠标的用户输入吗?提前致谢!
这是您所能做到的(至少对于标准库而言)。鼠标“跳跃”取决于系统,特别是取决于监听器的“采样率”。我不知道 JVM 参数会影响它,但如果有这种精神,我也不会感到惊讶。跳跃与鼠标加速度成相反关系(鼠标可以在样本之间移动“长”距离)。
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();
}
}
Edit:我刚刚有了一个想法,涉及绘制光标,使其看起来好像鼠标根本没有移动。如果我有什么工作,我会添加代码。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)