java - 用于设置鼠标速度的 JNA SystemParametersInfo 返回 false(未更改)

标签 java windows winapi jna

我正在使用 JNA 从 user32 调用 SystemParametersInfo。这是我的JNA接口(interface)方法:

boolean SystemParametersInfo(
        int uiAction,
        int uiParam,
        Pointer pvParam,
        int fWinIni
);

这是我的使用方法:

User32.INSTANCE.SystemParametersInfo(SPI_SETMOUSESPEED, 0,
    new IntByReference(2).getPointer(),
    SPIF_UPDATEINIFILE | SPIF_SENDCHANGE | SPIF_SENDWININICHANGE);

这应该将鼠标速度设置为 2(满分 20),但它没有任何效果,并且该方法始终返回 false。

这些是我使用的标志值:

private static final int SPI_GETMOUSESPEED = 0x70;
private static final int SPI_SETMOUSESPEED = 0x0071;
private static final int SPIF_UPDATEINIFILE = 0x01;
private static final int SPIF_SENDCHANGE = 0x02;
private static final int SPIF_SENDWININICHANGE = 0x02;

最佳答案

SystemParametersInfo() 的返回值是 BOOL,也称为 4 字节 int 的别名。因此,在 Java 端使用 int 而不是 boolean 作为返回值。

除此之外,SystemParametersInfo() 失败的原因是您没有正确传递速度值。仔细阅读SPI_SETMOUSESPEED文档:

SPI_SETMOUSESPEED
0x0071
Sets the current mouse speed. The pvParam parameter is an integer between 1 (slowest) and 20 (fastest). A value of 10 is the default. This value is typically set using the mouse control panel application.

将其与 SPI_GETMOUSESPEED 文档进行比较:

SPI_GETMOUSESPEED
0x0070
Retrieves the current mouse speed. The mouse speed determines how far the pointer will move based on the distance the mouse moves. The pvParam parameter must point to an integer that receives a value which ranges between 1 (slowest) and 20 (fastest). A value of 10 is the default. The value can be set by an end-user using the mouse control panel application or by an application using SPI_SETMOUSESPEED.

因此,即使 pvParam 参数被声明为指针,SPI_SETMOUSESPEED 需要的是实际整数值,而不是指针保存该值的整数,就像您当前使用 IntByReference.getPointer() 传递的一样。这个问题的答案证实了这一点(尽管是针对 C++,而不是 Java):

Mouse speed not changing by using SPI_SETMOUSESPEED

在C/C++中,解决方案是这样的:

SystemParametersInfo(SPI_SETMOUSESPEED, 0,
    (void*)2,
    SPIF_UPDATEINIFILE | SPIF_SENDCHANGE | SPIF_SENDWININICHANGE);

在 Java 中,等价的更像是这样:

User32.INSTANCE.SystemParametersInfo(SPI_SETMOUSESPEED, 0,
    Pointer.createConstant(2),
    SPIF_UPDATEINIFILE | SPIF_SENDCHANGE | SPIF_SENDWININICHANGE);

关于java - 用于设置鼠标速度的 JNA SystemParametersInfo 返回 false(未更改),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31522233/

相关文章:

java - 获取与虚拟机 (VCenter) 相关的所有事件

java - mongoDB 将数据添加到没有 _id 字段的集合

c# - 复制粘贴后台工具

c++ - 使用 C++ 从我的可执行文件中按其资源 ID 获取图标索引

winapi - 创建 "virtual"文件并执行

java - 有没有办法让 ExecutorService 递归工作?

java - 如何在基于 java 的 Web 应用程序中覆盖重复的 http 缓存 header ?

windows - 可以将可执行文件存储在程序数据中吗?

windows - 如何从 Windows 命令行中删除所有文本?

c++ - 将屏幕鼠标坐标转换为窗口坐标