简体   繁体   English

Java Awt Robot改变了Windows鼠标速度

[英]Java Awt Robot changes Windows Mouse Speed

Every time I use Robot to move the mouse, it resets the Windows mouse speed. 每次我使用Robot移动鼠标时,它都会重置Windows鼠标速度。 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". 这样做是essentaily点击开始按钮,键入“记事本”,打开记事本,然后键入“我正在写这个”。

robotType() is just a quick function I made that converts a string into a series of keyboard presses/releases. robotType()只是我做的一个快速函数,它将字符串转换为一系列键盘按下/释放。

This would appear to be a windows bug, as nothing you have done inherently alters the mouse speed. 这似乎是一个Windows bug,因为你所做的一切都不会改变鼠标的速度。 It seems you may be out of luck... 看来你可能运气不好......

Not a fix, but a workaround: 不是修复,而是一种解决方法:

With JNA you can get/set the mouse-speed (verify you are running on windows). 使用JNA,您可以获取/设置鼠标速度(验证您在Windows上运行)。 When your program is starting, read the mouse-speed. 程序启动时,请阅读鼠标速度。 Then after every robot.mouseMove() restore that value. 然后在每个robot.mouseMove()恢复该值之后。

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 你需要添加jna.jarjna.jar jna-platform.jar ,可以在这里找到: 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);
    }

    //[...]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM