我正在使用 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/