Java Awt Robot Changes Windows Mouse Speed

Every time I use Robot to move the mouse, it resets the speed of the Windows mouse. This is really annoying, and I was wondering if anyone knew how to fix this. Here is basically the code I come across:

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 does this mean, just click on the โ€œStartโ€ button, enter โ€œnotepadโ€, open notepad, then enter โ€œI am writing thisโ€.

robotType () is just a quick function I made that converts a string into a sequence of keystrokes / releases of the keyboard.

+6
source share
2 answers

This would seem to be a Windows error, since nothing that you actually changed changes the speed of the mouse. It seems you might be out of luck ...

+1
source

Not a fix, but a workaround:

With JNA, you can get / set the mouse speed (make sure you are running on Windows). When your program starts, read the mouse speed. Then after each robot.mouseMove() restore this value.

You 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); } //[...] } 
0
source

Source: https://habr.com/ru/post/912280/


All Articles