对于我的全屏应用程序,如果光标没有移动,我想在几秒钟后隐藏光标,例如 QuickTime 或 iTunes 中的全屏模式。大概我想打电话[NSCursor setHiddenUntilMouseMoves:YES]
,但我怎么知道什么时候打电话呢?
大概我正在寻找与空闲时间后隐藏鼠标光标但在Mac上相同的东西。我找不到一种方法来获得类似的“空闲时间”。(另外,我可能不关心键盘事件,只关心鼠标移动。)
对于我的全屏应用程序,如果光标没有移动,我想在几秒钟后隐藏光标,例如 QuickTime 或 iTunes 中的全屏模式。大概我想打电话[NSCursor setHiddenUntilMouseMoves:YES]
,但我怎么知道什么时候打电话呢?
大概我正在寻找与空闲时间后隐藏鼠标光标但在Mac上相同的东西。我找不到一种方法来获得类似的“空闲时间”。(另外,我可能不关心键盘事件,只关心鼠标移动。)
您可以使用以下方法获取光标(如果您还需要键盘)空闲的时间:
CGEventSourceSecondsSinceLastEventType(kCGEventSourceStateCombinedSessionState, kCGEventMouseMoved)
斯威夫特 3 代码:
CGEventSource.secondsSinceLastEventType(CGEventSourceStateID.combinedSessionState, eventType: CGEventType.mouseMoved)
另请参阅http://developer.apple.com/library/mac/#documentation/Carbon/Reference/QuartzEventServicesRef/Reference/reference.html。您可能必须每隔几秒钟轮询一次此函数,并且您应该假设如果返回的时间减少了用户移动了光标。
如何使用 NSTimer 并在 n 秒后检查是否没有发生任何事情?
您可以设置NSCursor.setHiddenUntilMouseMoves(true)
,例如:
import Cocoa
class MyWindowController: NSWindowController {
private var mouseTimer: Timer?
private var mouseTimeOut: Float = 1.0
override func awakeFromNib() {
mouseTimer = Timer.scheduledTimer(timeInterval: TimeInterval(mouseTimeOut),
target: self,
selector: #selector(hideMouse),
userInfo: nil,
repeats: true)
}
// MARK: - Mouse Cursor
@objc func hideMouse() {
if Float(CGEventSource.secondsSinceLastEventType(CGEventSourceStateID.combinedSessionState, eventType: CGEventType.mouseMoved)) > mouseTimeOut {
NSCursor.setHiddenUntilMouseMoves(true)
}
}
}