设置鼠标速度的 JNA SystemParametersInfo 返回 false(未更改)
JNA SystemParametersInfo to set mouse speed is returning false (not changing)
我正在使用 JNA 从 user32 调用 SystemParametersInfo。这是我的 JNA 接口方法:
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()
的 return 值是 BOOL
,也就是 4 字节 int
的别名。因此,在 Java 侧使用 int
而不是 boolean
作为 return 值。
除此之外,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);
我正在使用 JNA 从 user32 调用 SystemParametersInfo。这是我的 JNA 接口方法:
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()
的 return 值是 BOOL
,也就是 4 字节 int
的别名。因此,在 Java 侧使用 int
而不是 boolean
作为 return 值。
除此之外,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);