Swift 切换到其他窗口后,鼠标移动事件停止工作

Swift 切换到其他窗口后,鼠标移动事件停止工作,swift,macos,cocoa,Swift,Macos,Cocoa,acceptsMouseMovedEvents工作正常,但当“我的应用程序”窗口不在前面时,鼠标移动事件停止工作。如果我的窗户在前面,一切正常 我该怎么办?我希望我的应用程序可以在后台运行。您可以通过调用NSEvent.mouseLocation按需获取鼠标位置。这就是数字颜色计的工作原理(计时器+鼠标定位) OBJ示例 _timer = [NSTimer scheduledTimerWithTimeInterval:1

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


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

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

OBJ示例

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

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

您是否正在尝试从我们这边获取您的应用程序的点击?因为这是不可能的,我想要像macOS应用程序一样的功能-数字颜色计,我想有办法做到这一点。因为数字颜色计在后台运行时可以保持工作(RGB不断变化)Timer=Timer.scheduledTimer(时间间隔:1,重复:true,块:{Timer in var point:NSPoint=NSEvent.mouseLocation print((NSStringFromPoint(point)))})