我有一个 Mac SpriteKit/swift 应用程序(一个游戏),我需要将光标保持在一个窗口内。或者至少在它离开游戏窗口时给出一些警告。
我已经设置了一个跟踪区域,并且正在使用鼠标移动游戏 handle 。但是当光标移到窗口外时,桨停止工作。
let options = [ .mouseMoved,
.activeAlways,
.mouseEnteredAndExited] as NSTrackingArea.Options
let tracker = NSTrackingArea(rect:frame, options: options, owner:view, userInfo: nil)
view.addTrackingArea(tracker)
鼠标进入和退出功能似乎没有触发
override func mouseExited(with event: NSEvent) {
// NSCursor.unhide()
print("_____________________EXIT")
//Never fires
}
override func mouseEntered(with event: NSEvent) {
// NSCursor.hide()
print("_____________________ENTER")
// never fires
}
有什么想法吗?谢谢
最佳答案
我认为您会更喜欢不同的解决方案:隐藏光标并断开鼠标移动与光标位置的连接。
您可以使用 CGDisplayHideCursor
和 CGDisplayShowCursor
隐藏和显示指针。
您可以使用 CGAssociateMouseAndMouseCursorPosition
断开鼠标移动与光标位置的连接(并重新连接)。当您在鼠标与光标断开连接时收到 mouseMoved
和 mouseDragged
事件时,您可以从 deltaX
和 中读取鼠标移动>deltaY
事件的字段。
关于Swift:防止鼠标指针离开窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50357135/