如何移动(或使用)鼠标
如何使用 Java 和 JNA (Java Native Access) 与 Windows API 交互?我试图通过在鼠标输入流上排队鼠标事件来让鼠标执行某些操作,并且代码有效,因为 SendInput(...)
方法返回 1 表明它已成功将该事件排队,但鼠标本身什么也不做。
我的 SSCCE:
编辑: 已编辑以填写 dwFlags 字段。我已经尝试了几种常量的组合,无论是它们本身还是位或组合,仍然没有成功。同样,SendInput 方法返回 1,因为它应该建议一个有效方法,但鼠标不动:
import com.sun.jna.Native;
import com.sun.jna.platform.win32.WinDef.*;
import com.sun.jna.platform.win32.WinUser.*;
import com.sun.jna.win32.StdCallLibrary;
public class MouseUtils {
public interface User32 extends StdCallLibrary {
public static final long MOUSEEVENTF_MOVE = 0x0001L;
public static final long MOUSEEVENTF_VIRTUALDESK = 0x4000L;
public static final long MOUSEEVENTF_ABSOLUTE = 0x8000L;
User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class);
DWORD SendInput(DWORD dWord, INPUT[] input, int cbSize);
}
public static void main(String[] args) {
INPUT input = new INPUT();
input.type = new DWORD(INPUT.INPUT_MOUSE);
input.input.mi.dx = new LONG(500);
input.input.mi.dy = new LONG(500);
input.input.mi.mouseData = new DWORD(0);
input.input.mi.dwFlags = new DWORD(User32.MOUSEEVENTF_MOVE
| User32.MOUSEEVENTF_VIRTUALDESK | User32.MOUSEEVENTF_ABSOLUTE);
// input.input.mi.dwFlags = new DWORD(0x8000L);
input.input.mi.time = new DWORD(0);
INPUT[] inArray = {input};
int cbSize = input.size(); // mouse input struct size
DWORD nInputs = new DWORD(1); // number of inputs
DWORD result = User32.INSTANCE.SendInput(nInputs , inArray, cbSize);
System.out.println("result: " + result); // return 1 if the 1 event successfully inserted
}
}
编辑 2:
进行更多阅读,看来我对 JNA 数组的理解是有缺陷的,我必须从 C 数组的角度来思考,其中数组只是指向连续内存区域的指针。更多即将到来(我希望!)。
How to interact with the Windows API using Java and the JNA (Java Native Access)?. I'm trying to make the mouse do something by queuing a mouse event on the mouse input stream, and the code works, in that the SendInput(...)
method returns 1 suggesting that it has successfully queued the event, but yet the mouse itself does nothing.
My SSCCE:
Edit: edited to fill in the dwFlags field. I've tried several combinations of constants either by themselves or bit-or combined without success still. Again, the SendInput method returns 1 as it should suggesting a functioning method, but the mouse doesn't budge:
import com.sun.jna.Native;
import com.sun.jna.platform.win32.WinDef.*;
import com.sun.jna.platform.win32.WinUser.*;
import com.sun.jna.win32.StdCallLibrary;
public class MouseUtils {
public interface User32 extends StdCallLibrary {
public static final long MOUSEEVENTF_MOVE = 0x0001L;
public static final long MOUSEEVENTF_VIRTUALDESK = 0x4000L;
public static final long MOUSEEVENTF_ABSOLUTE = 0x8000L;
User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class);
DWORD SendInput(DWORD dWord, INPUT[] input, int cbSize);
}
public static void main(String[] args) {
INPUT input = new INPUT();
input.type = new DWORD(INPUT.INPUT_MOUSE);
input.input.mi.dx = new LONG(500);
input.input.mi.dy = new LONG(500);
input.input.mi.mouseData = new DWORD(0);
input.input.mi.dwFlags = new DWORD(User32.MOUSEEVENTF_MOVE
| User32.MOUSEEVENTF_VIRTUALDESK | User32.MOUSEEVENTF_ABSOLUTE);
// input.input.mi.dwFlags = new DWORD(0x8000L);
input.input.mi.time = new DWORD(0);
INPUT[] inArray = {input};
int cbSize = input.size(); // mouse input struct size
DWORD nInputs = new DWORD(1); // number of inputs
DWORD result = User32.INSTANCE.SendInput(nInputs , inArray, cbSize);
System.out.println("result: " + result); // return 1 if the 1 event successfully inserted
}
}
Edit 2:
Doing more reading, and it seems that my understanding of arrays with JNA is deficient, that I have to think in terms of C arrays where an array is simply a pointer to a region of contiguous memory. More to come (I hope!).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
JNA 文档 使用结构和联合 内容如下:
我猜你错过了
setType
部分。此外,当使用MOUSEEVENTF_ABSOLUTE
时,dx
和dy
被指定为鼠标坐标,而不是像素。以下作品:
JNA document Using Structures And Unions reads:
I guess you missed
setType
part. Also, when usingMOUSEEVENTF_ABSOLUTE
,dx
anddy
are specified as the coordinate of the mouse, not pixels.Following works:
对结构调用 toArray() 方法以获得连续的内存块。
或者,您可以简单地为 SendInput 声明一个替代方法映射:
但是,可能还会发生其他情况(也许是权限?请参阅 UIPI 上的 MS 注释),因为您的示例应该可以工作(至少对于单个数组元素)。
编辑: Union.setType() 答案确实是正确的。
Call the toArray() method on your structure to obtain a contiguous block of memory.
Alternatively, you can simply declare an alternate method mapping for SendInput:
However, there may be something else going on (permissions, perhaps? see MS notes on UIPI), since your example ought to work (at least with a single array element).
EDIT: the Union.setType() answer is indeed the correct one.
您没有指定任何鼠标输入标志,因此没有鼠标输入。
You didn't specify any of the mouse input flags, so there was no mouse input.