SendInput通过Java本地访问无法正确执行鼠标移动。

huangapple go评论98阅读模式
英文:

SendInput won't execute correctly via Java Native Access for mouse movement

问题

Code to be translated to Java

  1. #include<windows.h>
  2. #include<iostream>
  3. using namespace std;
  4. void moveMouse(int x, int y) {
  5. INPUT input;
  6. input.type = INPUT_MOUSE;
  7. input.mi.dx = x;
  8. input.mi.dy = y;
  9. input.mi.time = 0;
  10. input.mi.dwFlags = MOUSEEVENTF_MOVE;
  11. UINT qwe = SendInput(1, &input, sizeof(input));
  12. cout<< qwe;
  13. }

Code in Java using JNA

  1. import com.sun.jna.platform.win32.User32;
  2. import com.sun.jna.platform.win32.WinDef.DWORD;
  3. import com.sun.jna.platform.win32.WinDef.LONG;
  4. import com.sun.jna.platform.win32.WinUser.INPUT;
  5. import static com.sun.jna.platform.win32.WinUser.INPUT.INPUT_MOUSE;
  6. public class Test {
  7. public static void main(String[] a) {
  8. moveMouse(
  9. 1_000L,
  10. 1_000L
  11. );
  12. }
  13. public static void moveMouse(Long x, Long y) {
  14. INPUT input = new INPUT();
  15. input.type = new DWORD(INPUT_MOUSE);
  16. input.input.mi.dx = new LONG(x);
  17. input.input.mi.dy = new LONG(y);
  18. input.input.mi.time = new DWORD(0);
  19. input.input.mi.dwFlags = new DWORD(0x0001L);
  20. INPUT[] inputArray = {input};
  21. DWORD result = User32.INSTANCE.SendInput(new DWORD(1), inputArray, input.size());
  22. System.out.println("result = " + result.longValue());
  23. System.out.println("size = " + input.size());
  24. }
  25. }

Output

  1. result = 1
  2. size = 40

The Java code attempts to replicate the C++ SendInput() function using JNA. However, the code currently sends the size of the input variable instead of the array's size, resulting in the mouse not moving when executed. The attempt to use sizeof to determine the array size in bytes causes an error due to restricted access to a private function.

英文:

Intent

I am trying to create a localized implementation of cloud gaming where a user's own PC serves as the server. What I am trying to achieve is the last piece stopping me. That is implementing hardware mouse movements. This is where Windows' SendInput() comes into play.

Issue

The issue I have right now is that My entire code is based on Kotlin/Java. So, I don't exactly know how to replicate the functions of SendInput() in Java other than to use JNA to access C++ functions. More specifically access SendInput(). But, here's where I'm stuck. My Java code compiles, but doesn't execute when called.

Code to be translated to Java

  1. #include<windows.h>
  2. #include<iostream>
  3. using namespace std;
  4. void moveMouse(int x, int y) {
  5. INPUT input;
  6. input.type = INPUT_MOUSE;
  7. input.mi.dx = x;
  8. input.mi.dy = y;
  9. input.mi.time = 0;
  10. input.mi.dwFlags = MOUSEEVENTF_MOVE;
  11. UINT qwe = SendInput(1, &input, sizeof(input));
  12. cout<< qwe;
  13. }

Code in Java using JNA

look into the comments in the code below.

  1. import com.sun.jna.platform.win32.User32;
  2. import com.sun.jna.platform.win32.WinDef.DWORD;
  3. import com.sun.jna.platform.win32.WinDef.LONG;
  4. import com.sun.jna.platform.win32.WinUser.INPUT;
  5. //import static com.sun.jna.Native.sizeof;//compilation error 'sizeof(int)' has private access in 'com.sun.jna.Native'
  6. import static com.sun.jna.platform.win32.WinUser.INPUT.INPUT_MOUSE;
  7. public class Test {
  8. public static void main(String[] a) {
  9. moveMouse(
  10. 1_000L,
  11. 1_000L
  12. );
  13. }
  14. /**
  15. * @param x change required in x co-ordinate
  16. * @param y change required in y co-ordinate
  17. *
  18. */
  19. public static void moveMouse(Long x, Long y) {
  20. INPUT input = new INPUT();
  21. input.type = new DWORD(INPUT_MOUSE);
  22. input.input.mi.dx = new LONG(x);
  23. input.input.mi.dy = new LONG(y);
  24. input.input.mi.time = new DWORD(0);
  25. input.input.mi.dwFlags = new DWORD(0x0001L);
  26. INPUT[] inputArray = {input};
  27. DWORD result = User32.INSTANCE.SendInput(new DWORD(1), inputArray, input.size());
  28. // sizeof (used below in the commented code) returns the compilation error
  29. // 'sizeof(int)' has private access in 'com.sun.jna.Native'
  30. // I got the idea to use sizeof from another StackOverflow post
  31. // DWORD result = User32.INSTANCE.SendInput(new DWORD(1), inputArray, sizeof(input));
  32. System.out.println("result = " + result.longValue());
  33. System.out.println("size = " + input.size());
  34. }
  35. }

Output

  1. result = 1
  2. size = 40

You'll see that in SendInput, I'm sending the size of the input variable instead of the array's size which is what I'm supposed to send. To get the size of array in bytes, I got the idea to use sizeof, but, as you have seen above in the comments, you'll have already understood why I can't use it as I can't import a private function.

The mouse doesn't move a pixel when the Java code is executed.

答案1

得分: 0

  1. 我忘记了设置网络类型没有意识到这是必要的无论如何这是代码 -
  2. public static void moveMouse(Long x, Long y) {
  3. INPUT input = new INPUT();
  4. input.type = new DWORD(INPUT_MOUSE);
  5. input.input.setType("mi");//--------------------------------------added this
  6. input.input.mi.dx = new LONG(x);
  7. input.input.mi.dy = new LONG(y);
  8. input.input.mi.time = new DWORD(0);
  9. input.input.mi.dwFlags = new DWORD(0x0001L);
  10. INPUT[] inputArray = {input};
  11. DWORD result = User32.INSTANCE.SendInput(new DWORD(1), inputArray, input.size());
  12. }
  13. 对于遇到此问题的任何人此函数可以直接复制粘贴应该可以工作
英文:

I forgot to net type. Didn't realize this was necessary. Anyways, here is the code -

  1. public static void moveMouse(Long x, Long y) {
  2. INPUT input = new INPUT();
  3. input.type = new DWORD(INPUT_MOUSE);
  4. input.input.setType("mi");//--------------------------------------added this
  5. input.input.mi.dx = new LONG(x);
  6. input.input.mi.dy = new LONG(y);
  7. input.input.mi.time = new DWORD(0);
  8. input.input.mi.dwFlags = new DWORD(0x0001L);
  9. INPUT[] inputArray = {input};
  10. DWORD result = User32.INSTANCE.SendInput(new DWORD(1), inputArray, input.size());
  11. }

For anybody having this issue, this function can be straight up copy pasted and it should work.

huangapple
  • 本文由 发表于 2023年2月16日 17:07:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75469943.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定