切换到其他 windows 后鼠标移动事件停止工作

Mouse move events stop working after switching to other windows

acceptsMouseMovedEvents 工作正常,但是当我的应用程序 window 不在前面时 ,鼠标移动事件停止工作。如果我的 window 在前面,一切正常

我该怎么办?我希望我的应用程序可以 运行 在后台。

调用NSEvent.mouseLocation可以按需获取鼠标位置。这就是数字色度计的工作原理(定时器 + 鼠标定位)

对象示例

 _timer = [NSTimer scheduledTimerWithTimeInterval:1
                                          repeats:YES block:^(NSTimer * _Nonnull timer) {
                                           NSPoint point = [NSEvent mouseLocation];
                                           NSLog(@"%@", NSStringFromPoint(point));
                                     }];

SWIFT:

var timer = Timer.scheduledTimer(withTimeInterval: 1,
                                 repeats: true,
                                 block: { timer in let point: NSPoint = NSEvent.mouseLocation
                                    print("(NSStringFromPoint\(point))")
})