![](/img/trans.png)
[英]JNA / WinAPI. Simulating mouse click moves mouse cursor and doesn't return it back to the start position
[英]JNA / WinAPI. Simulate mouse click without moving the cursor doesn't work correctly
编辑:对不起,但我不确定我的问题是否正确关闭。 我被建议这个线程,但它没有回答我的问题。 我能够模拟鼠标点击,但正如我在问题中描述的那样,它不能正常工作。
我仍在学习 JNA 并在我的Java应用程序(JNA 5.6.0 和 jna-platform 5.6.0)中使用它,但我希望熟悉C语言的人也能理解我,因为 JNA 使用的是 ZA82560B798Z012A 函数我的操作系统是 Windows 10。
我有的:
LowLevelKeyboardProc()
并调用click()
方法的低级键盘钩子,如下所述。问题是我无法正确执行鼠标点击游戏 window 的坐标。
我要提前声明,我不违反游戏许可协议的规则,并且仅出于个人目的使用旧版游戏,1.26。 另外,我在其他编程语言中也看到过类似的实现,但我想在 Java 中实现它。
下面我附上我尝试过的 3 个选项,以及问题的描述:
1 . 使用User32.INSTANCE.SendMessage()
public void click(KeyBindComponent keyBindComponent) {
final int WM_LBUTTONDOWN = 513;
final int WM_LBUTTONUP = 514;
final int MK_LBUTTON = 0x0001;
Map<String, Integer> cords = getCords(keyBindComponent);
if (!cords.isEmpty()) {
int xCord = cords.get("width");
int yCord = cords.get("height");
LPARAM lParam = makeLParam(xCord, yCord);
user32Library.SendMessage(warcraft3hWnd, WM_LBUTTONDOWN, new WPARAM(MK_LBUTTON), lParam);
user32Library.SendMessage(warcraft3hWnd, WM_LBUTTONUP, new WPARAM(MK_LBUTTON), lParam);
System.out.println("x = " + xCord + " y = " + yCord);
}
}
public static LPARAM makeLParam(int l, int h) {
// note the high word bitmask must include L
return new LPARAM((l & 0xffff) | (h & 0xffffL) << 16);
}
预计会在测试坐标点(建筑物上)上进行不可见的点击。 但问题是该区域被分配了。 我假设执行了以下顺序:在当前鼠标 position中单击鼠标,然后将 cursor 移动到单击的坐标点。 但我不知道为什么会这样。
2.使用User32.INSTANCE.PostMessage()
public void click(KeyBindComponent keyBindComponent) {
final int WM_LBUTTONDOWN = 513;
final int WM_LBUTTONUP = 514;
Map<String, Integer> cords = getCords(keyBindComponent);
if (!cords.isEmpty()) {
int xCord = cords.get("width");
int yCord = cords.get("height");
LPARAM lParam = makeLParam(xCord, yCord);
user32Library.PostMessage(warcraft3hWnd, WM_LBUTTONDOWN, new WPARAM(0), lParam);
user32Library.PostMessage(warcraft3hWnd, WM_LBUTTONUP, new WPARAM(0), lParam);
System.out.println("x = " + xCord + " y = " + yCord);
}
}
public static LPARAM makeLParam(int l, int h) {
// note the high word bitmask must include L
return new LPARAM((l & 0xffff) | (h & 0xffffL) << 16);
}
发生了同样的情况,不是点击坐标,而是选择了区域,以及在SendMessage()
的情况下,我可能不会重新附加图片两次。
3.使用User32.INSTANCE.SendInput()
public void click(KeyBindComponent keyBindComponent) {
Map<String, Integer> cords = getCords(keyBindComponent);
if (!cords.isEmpty()) {
int xCord = cords.get("width");
int yCord = cords.get("height");
mouseMove(xCord, yCord);
mouseClick();
System.out.println("x = " + xCord + " y = " + yCord);
}
}
void mouseMove(int x, int y) {
final int MOUSEEVENTF_LEFTUP = 0x0004;
final int MOUSEEVENTF_ABSOLUTE = 0x8000;
INPUT input = new INPUT();
INPUT[] move = (INPUT[]) input.toArray(2);
// Release the mouse before moving it
move[0].type = new DWORD(INPUT.INPUT_MOUSE);
move[0].input.setType("mi");
move[0].input.mi.dwFlags = new DWORD(MOUSEEVENTF_LEFTUP);
move[0].input.mi.dwExtraInfo = new BaseTSD.ULONG_PTR(0);
move[0].input.mi.time = new DWORD(0);
move[0].input.mi.mouseData = new DWORD(0);
move[1].type = new DWORD(INPUT.INPUT_MOUSE);
move[1].input.mi.dx = new LONG(x);
move[1].input.mi.dy = new LONG(y);
move[1].input.mi.mouseData = new DWORD(0);
move[1].input.mi.dwFlags = new DWORD(MOUSEEVENTF_LEFTUP + MOUSEEVENTF_ABSOLUTE);
user32Library.SendInput(new DWORD(2), move, move[0].size());
}
void mouseClick() {
final int MOUSEEVENTF_LEFTUP = 0x0004;
final int MOUSEEVENTF_LEFTDOWN = 0x0002;
INPUT input = new INPUT();
INPUT[] click = (INPUT[]) input.toArray(2);
click[0].type = new DWORD(INPUT.INPUT_MOUSE);
click[0].input.setType("mi");
click[0].input.mi.dwFlags = new DWORD(MOUSEEVENTF_LEFTDOWN);
click[0].input.mi.dwExtraInfo = new BaseTSD.ULONG_PTR(0);
click[0].input.mi.time = new DWORD(0);
click[0].input.mi.mouseData = new DWORD(0);
click[1].type = new DWORD(INPUT.INPUT_MOUSE);
click[1].input.setType("mi");
click[1].input.mi.dwFlags = new DWORD(MOUSEEVENTF_LEFTUP);
click[1].input.mi.dwExtraInfo = new BaseTSD.ULONG_PTR(0);
click[1].input.mi.time = new DWORD(0);
click[1].input.mi.mouseData = new DWORD(0);
user32Library.SendInput(new DWORD(2), click, click[0].size());
}
在这种情况下,坐标点上根本没有点击。 相反,当按下某些键时,会在当前鼠标 position中单击鼠标。
顺便说一句,我也尝试过使用 Java Robot ,但它对我不起作用。 不幸的是,鼠标 cursor 从起始 position 移动(消失)大约一毫秒到您需要单击并返回起始 position 的点。
感谢您阅读到最后,对于如此繁琐的解释,我深表歉意。
谁能告诉我我在代码中犯了什么错误以及在哪里犯了错误? 由于在所有 3 个选项中,我都没有达到预期的行为。
对于第三种情况,您没有使用MOUSEEVENTF_MOVE
标志来移动鼠标,因此鼠标实际上并没有移动。 而且还根据文件:
如果指定
MOUSEEVENTF_ABSOLUTE
值,则dx和dy包含 0 到 65,535 之间的归一化绝对坐标
void mouseMove(int x, int y) {
INPUT move[2] = {};
DWORD fScreenWidth = ::GetSystemMetrics(SM_CXSCREEN);
DWORD fScreenHeight = ::GetSystemMetrics(SM_CYSCREEN);
move[0].type = move[1].type = INPUT_MOUSE;
move[0].mi.dwFlags = MOUSEEVENTF_LEFTUP;// Release the mouse before moving it
move[1].mi.dx = MulDiv(x, 65535, fScreenWidth);
move[1].mi.dy = MulDiv(y, 65535, fScreenHeight);
move[1].mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
SendInput(2, move, sizeof(INPUT));
}
然后使用MOUSEEVENTF_LEFTDOWN
和MOUSEEVENTF_LEFTUP
单击当前位置。
或者可以直接将鼠标移动合并到点击事件中:
void mouseMoveClick(int x, int y) {
INPUT click[3] = {};
click[0].type = INPUT_MOUSE;
click[0].mi.dwFlags = MOUSEEVENTF_LEFTUP;// Release the mouse before moving it
DWORD fScreenWidth = ::GetSystemMetrics(SM_CXSCREEN);
DWORD fScreenHeight = ::GetSystemMetrics(SM_CYSCREEN);
click[1].type = INPUT_MOUSE;
click[1].mi.dx = click[2].mi.dx= MulDiv(x, 65535, fScreenWidth);
click[1].mi.dy = click[2].mi.dy= MulDiv(y, 65535, fScreenHeight);
click[1].mi.dwFlags = MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
click[2].type = INPUT_MOUSE;
click[2].mi.dwFlags = MOUSEEVENTF_LEFTUP | MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
SendInput(3, click, sizeof(INPUT));
}
如果想鼠标点击后移动回原来的position,可以在移动前用GetCursorPos
记录当前的position。 然后使用 mouseMove 事件或更简单的SetCursorPos
返回到 position。
void click(int xCord, int yCord) {
//mouseMove(xCord, yCord);
POINT p = {};
GetCursorPos(&p);
mouseMoveClick(xCord, yCord);
SetCursorPos(p.x, p.y);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.