kwhat / jnativehook

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

The value of When is incorrect #376

Closed liuchengts closed 2 years ago

liuchengts commented 3 years ago

NativeKeyEvent getWhen() is incorrect for events triggered by keys

Environment: JDK version OpenJDK 11.0.8 Operating system macOS Bigsur Jnativehook version 2.2.0

The relevant code

public void nativeKeyPressed(NativeKeyEvent e) {
        logger.debug(System.currentTimeMillis() + "   " + e.getWhen());
    }

Output result 1635239195931 507209383807953 The value obtained by When does not apply to any form of date conversion 507209383807953 convert 18042-10-29 07:30:07

kwhat commented 3 years ago

It isn't a date, it is the native ordering. It use to be the current date but I changed it to save on resources, maybe I should change it back. The major concern was that two events could in theory have the same when.

liuchengts commented 3 years ago

I think we can use the occurrence time stamp directly here as a sort, because the normal sequence of events is always understood based on time.

I have fully understood the meaning of the current method return, I will deal with this time by myself before there is any action, thank you.

kwhat commented 2 years ago

I am going to switch it back at some point soon.

liuchengts commented 2 years ago

这是来自QQ邮箱的假期自动回复邮件。   您好,我最近正在休假中,无法亲自回复您的邮件。我将在假期结束后,尽快给您回复。

liuchengts commented 2 years ago

我很快就会把它换回来。

Okay, thank you very much!

kwhat commented 2 years ago

Ok, I have looked at this and there was a reason I switched away from using epoch time for the event timestamps. The issue is that if events are delivered faster than 1 per second, you do not get a unique time, for example:

id=9,when=1645116084363,mask=0x2002,x=1055,y=1772,button=0,clicks=0
id=9,when=1645116084365,mask=0x2002,x=1054,y=1772,button=0,clicks=0
id=9,when=1645116084366,mask=0x2002,x=1053,y=1773,button=0,clicks=0
id=9,when=1645116084370,mask=0x2002,x=1052,y=1773,button=0,clicks=0
id=9,when=1645116084377,mask=0x2002,x=1052,y=1773,button=0,clicks=0
id=9,when=1645116084377,mask=0x2002,x=1051,y=1773,button=0,clicks=0
id=9,when=1645116084377,mask=0x2002,x=1050,y=1774,button=0,clicks=0
id=9,when=1645116084377,mask=0x2002,x=1050,y=1774,button=0,clicks=0
id=9,when=1645116084377,mask=0x2002,x=1049,y=1775,button=0,clicks=0

I am not sure how much of a problem this would be. It's been a while since I have looked at Java's AWT events. Do they do the same thing with duplicate times for events that happen within the same second? Let me know and maybe we can do the same thing.

liuchengts commented 2 years ago

这是来自QQ邮箱的假期自动回复邮件。   您好,我最近正在休假中,无法亲自回复您的邮件。我将在假期结束后,尽快给您回复。

kwhat commented 2 years ago

https://github.com/kwhat/libuiohook/pull/113 should cover this feature. Please get back to me about the AWT event times.

liuchengts commented 2 years ago

一秒内发生的事件重复时间做同样的事情

It is true that there is a case of doing something multiple times in 1 second, but this repetition is not code behavior, but operator behavior, such as moving the mouse, or quickly pressing the keyboard

I move the mouse normally, in order to capture the track of each mouse movement. Here is my listening code and listening result

 public void nativeMouseMoved(NativeMouseEvent e) {
        logger.info("===================== id:{} when:{} second:{} x:{} y:{}", e.getID(), e.getWhen(), LocalTime.now().getSecond(), e.getX(), e.getY());
    }
===================== id:2503 when:163111093 second:35 x:915 y:373
===================== id:2503 when:163111109 second:35 x:921 y:373
===================== id:2503 when:163111109 second:35 x:929 y:373
===================== id:2503 when:163111125 second:35 x:941 y:373
===================== id:2503 when:163111125 second:35 x:951 y:373
===================== id:2503 when:163111140 second:35 x:959 y:373
===================== id:2503 when:163111140 second:35 x:965 y:374
===================== id:2503 when:163111156 second:35 x:970 y:375
===================== id:2503 when:163111156 second:35 x:975 y:377
===================== id:2503 when:163111171 second:35 x:976 y:378
===================== id:2503 when:163111171 second:35 x:976 y:380
===================== id:2503 when:163111187 second:35 x:976 y:382
===================== id:2503 when:163111187 second:35 x:976 y:388

I think we should leave the timestamp of the event occurrence in this listener, so that the program can restore the order of events as accurately as possible. For the case of occurrence within 1 second, it is recommended to use a higher precision time to represent, such as microsecond, nanosecond, etc. However, my current solution here is to use LocalDateTime to get the current time when the event is notified, and after all the events are recorded, the events are sorted according to the time of LocalDateTime to restore the correct order of operations, so that we don't have to consider how many times the events occurred in 1 second.

liuchengts commented 2 years ago

kwhat/libuiohook#113应该涵盖此功能。请回复我有关 AWT 活动时间的信息。

Is the above response the information you need? I may not have fully understood what you needed

kwhat commented 2 years ago

This has been resolved in the latest snapshot.