2

Is there a way to lock the mouse in one position in Java for a certain amount of time?

I've tried this:

while(timer == true){

    Robot bot = new Robot();
    bot.mouseMove(x, y);

}

But when the user moves the mouse it jumps unpleasantly back and forth (from the position the user is dragging to the position where it's supposed to be locked).

Any ideas if there is a better way to do this? Or can I completely disable user input for the mouse? Thanks in advance!

Ood
  • 610
  • 2
  • 10
  • 27
  • 6
    Why would you want to disable mouse movement? I'll not even think about helping, as I do **never** want a program that intentionally limits the user... – tilpner May 18 '14 at 18:07
  • I can't think of anyway you can completely disable mouse movement. try looking into MouseMovementListeners http://docs.oracle.com/javase/tutorial/uiswing/events/mousemotionlistener.html – SpaceCowboy May 18 '14 at 18:09
  • It's not supposed limit the user, it's only temporarily. – Ood May 18 '14 at 18:12
  • 3
    @StackOverflowException Limiting mouse motion obviously isn't a good idea for most programs, but this doesn't make it inherently evil to implement depending on why it's being done. By the same logic, this post also shouldn't be answered: http://stackoverflow.com/questions/6470651/creating-a-memory-leak-with-java?rq=1 It's rather condescending to tell people "I'll not even think about helping" because _you_ don't need to do this. – SimonT May 18 '14 at 18:12
  • 1
    @SimonT: Probably. I just can't think of any good reason to limit the user, even for half a second. – tilpner May 18 '14 at 18:13
  • 1
    @tilpner think about games like minecraft, those games limit mouse movement so that your mouse doesnt go off the screen while playing. op is probably just trying to do something like that – madladzen Apr 23 '20 at 22:04

1 Answers1

3

This is as far as you can go (at least with the standard libraries). The mouse "jumps" are system dependent, specifically on the "sampling rate" of the listener. I'm not aware of a JVM parameter affecting it, but wouldn't be surprised if there is something in that spirit. The jumps are in opposite relation to the mouse acceleration (the mouse can move a "long" distance between the samples).

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: I have just gotten an idea involving painting of the cursor to appear as if the mouse is not moving at all. I'll add code if I get something working.

user1803551
  • 11,914
  • 5
  • 39
  • 63