kwhat / jnativehook

Global keyboard and mouse listeners for Java.
Other
1.75k stars 348 forks source link

The problem of calculating mouse coordinates #398

Open liuchengts opened 2 years ago

liuchengts commented 2 years ago

Environment: JDK version OpenJDK 11.0.8 Operating system win11 x64 Professional system Jnativehook version 2.2.0

On win11 x64 Professional system, the mouse x,y coordinates obtained in NativeMouseEvent event are not restored to the x,y coordinates position correctly, they are not the actual coordinates of the current system. But there is no such problem on macOS Bigsur.

Please note that this problem occurs in all nativeMouse* methods

Sample Code:

public class GlobalListener implements  NativeKeyListener, NativeMouseInputListener, NativeMouseWheelListener {
    public void nativeMouseMoved(NativeMouseEvent e) {
            logger.info(e.getX(),e.getY());
    }
    //param e.getX(),e.getY()
    public  void test(){
         Robot robot = new Robot();
          robot.delay(5000);
          Point p = MouseInfo.getPointerInfo().getLocation();
          int width = (int) p.getX();
          int heigh = (int) p.getY();
          System.out.println(width + ":" + heigh);
          robot.mouseMove(width ,heigh );
          //robot.mouseMove(804,438);
    }
}

My temporary solution is to get the x, y coordinates of the mouse again after receiving this event with the following code

public void nativeMouseMoved(NativeMouseEvent e) {
          getMouse();
}
MouseModel getMouse() {
        Point p = MouseInfo.getPointerInfo().getLocation();
        return new MouseModel((int) Math.floor(p.getX() + 0.5), (int) Math.floor(p.getY() + 0.5));
}
kwhat commented 2 years ago

event are not restored to the x,y coordinates position correctly, they are not the actual coordinates of the current system.

I don't really understand what problem you are experiencing. What do you mean they are not the actual coordinates of the current system? Can you provide examples of x,y you received and the x,y you expected?

Do you know if this is a problem with Windows 10?

Have you tested with the latest snapshot?

liuchengts commented 2 years ago

event are not restored to the x,y coordinates position correctly, they are not the actual coordinates of the current system.

I don't really understand what problem you are experiencing. What do you mean they are not the actual coordinates of the current system? Can you provide examples of x,y you received and the x,y you expected?

Do you know if this is a problem with Windows 10?

Have you tested with the latest snapshot?

The coordinates of the version I am currently using are as follows:

<dependency>
            <groupId>com.github.kwhat</groupId>
            <artifactId>jnativehook</artifactId>
            <version>2.2.1</version>
</dependency>

Problem Description: When the system is windows 11 x64, the mouse x,y coordinate values obtained by [NativeMouseInputListener, NativeMouseWheelListener] cannot move the mouse correctly when executed by the java built-in method new Robot().mouseMove(x,y). mouseMove(x,y) method of java can't move the mouse to the position it was supposed to reach.

For example:

public void nativeMouseMoved(NativeMouseEvent e) {
            logger.info(e.getX(),e.getY());
          //Get the mouse x, y coordinate values
         Robot robot = new Robot();
         robot.mouseMove(e.getX(),e.getY());
        //will not be able to restore the mouse to the preposition, and of course the reverse is also true
    }
liuchengts commented 2 years ago

event are not restored to the x,y coordinates position correctly, they are not the actual coordinates of the current system.

I don't really understand what problem you are experiencing. What do you mean they are not the actual coordinates of the current system? Can you provide examples of x,y you received and the x,y you expected?

Do you know if this is a problem with Windows 10?

Have you tested with the latest snapshot?

Sorry, I'm not sure if it's a windows problem, I don't have this problem on other systems, but this problem appears on all windows around me