输入事件的 NSEvent 时间戳的准确性 (keyboard/mouse)?

Accuracy of NSEvent timestamp for input events (keyboard/mouse)?

在 Cocoa 中,NSEvents 有一个时间戳 属性,其中 returns 代表 "The time when the event occurred in seconds since system startup." 的时间间隔按键。但我担心时间戳可能不够准确。 Timeinterval 本身具有亚毫秒精度,这很棒。是否有任何文档表明亚毫秒精度是否用于为表示键盘和鼠标输入的 NSEvent 提供准确的时间戳?

如果我不得不猜测 apple 的实现,我希望 NSEvent 上的时间戳可以在硬件中断后立即生成,这太棒了。然而,我也可以想象一个系统轮询 keyboard/mouse 输入,并且仅在下一个轮询间隔到来时填充时间戳字段并读取输入。

非常感谢您的任何见解。

您可能会发现 Apple 的 Cocoa Event Handling Guide: Event Architecture 文档很有用。来自:

Before it dispatches an event to an application, the window server processes it in various ways; it time-stamps it, annotates it with the associated window and process port, and possibly performs other tasks as well.

这似乎表明位于内核和应用层之间的 Window 服务器应用了时间戳...尽管它可能会读取在较低级别生成的元数据以便这样做。

我认为更好地理解这一点的最佳方法是编写一个应用程序来记录关键 down/up 事件的时间戳并在相邻事件中查找精确匹配。如果您连续看到多个事件具有(几乎)相同的时间戳,则它们更有可能在获得时间戳之前在某个缓冲区中排队。如果相邻事件之间有足够的间隙,则它更有可能在更接近硬件事件的地方被标记。