Java Awt Robot changes Windows Mouse Speed
Asked Answered
S

2

6

Every time I use Robot to move the mouse, it resets the Windows mouse speed. This is really annoying to deal with, and I was wondering if anyone knows how to fix this. Here is basically the code I am messing around with:

Robot robot = new Robot();
robot.mouseMove(10, 1070);
robot.delay(300);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(300);
robotType("notepad");
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.delay(400);
robotType("I am writing this.");

What this does is essentaily click the start button, type "notepad", open notepad, then types "I am writing this".

robotType() is just a quick function I made that converts a string into a series of keyboard presses/releases.

Shiau answered 3/4, 2012 at 2:42 Comment(0)
B
1

This would appear to be a windows bug, as nothing you have done inherently alters the mouse speed. It seems you may be out of luck...

Bore answered 23/7, 2012 at 3:45 Comment(0)
A
0

Not a fix, but a workaround:

With JNA you can get/set the mouse-speed (verify you are running on windows). When your program is starting, read the mouse-speed. Then after every robot.mouseMove() restore that value.

You'll need to add jna.jar and jna-platform.jar which can be found here: https://github.com/java-native-access/jna/tree/master/dist

interface User32 extends com.sun.jna.platform.win32.User32 {

    User32 INSTANCE = (User32) Native.loadLibrary(User32.class,
            W32APIOptions.DEFAULT_OPTIONS);

    boolean SystemParametersInfo(
            int uiAction,
            int uiParam,
            Object pvParam, // Pointer or int
            int fWinIni
    );
}

public static void main(String[] args) throws AWTException {
    Pointer mouseSpeedPtr = new Memory(4);
    Integer mouseSpeed = User32.INSTANCE.SystemParametersInfo(0x0070, 0, mouseSpeedPtr, 0)
            ? mouseSpeedPtr.getInt(0) : null;

    //[...]

    rob.mouseMove(10, 1070);
    if (mouseSpeed != null) {
        User32.INSTANCE.SystemParametersInfo(0x0071, 0, mouseSpeed, 0x02);
    }

    //[...]
}
Archil answered 8/1, 2017 at 1:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.