views:

102

answers:

2

For my fullscreen app, I want to hide the cursor after a few seconds if it's not moved, like the fullscreen mode in QuickTime or iTunes. Presumably I'm looking to call [NSCursor setHiddenUntilMouseMoves:YES], but how do I know when to call it?

Presumably I'm looking for the same as http://stackoverflow.com/questions/744980/hide-mouse-cursor-after-an-idle-time but on the Mac. I couldn't find a way to get similar a "idle time". (Plus, I probably don't care about keyboard events, just mouse movement.)

+1  A: 

What about using NSTimer and check after n seconds whether nothing has happened?

John Smith
But how can you tell whether nothing has happened?
Ben Alpert
Ben Alpert: When something does happen, set a flag. When the timer fires, check the flag. If you haven't set it, nothing happened.
Peter Hosey
+1  A: 

You can get the time the cursor (and if you want also the keyboard) has been idle using: CGEventSourceSecondsSinceLastEventType(kCGEventSourceStateCombinedSessionState, kCGEventMouseMoved)

See also http://developer.apple.com/library/mac/#documentation/Carbon/Reference/QuartzEventServicesRef/Reference/reference.html. You're probably going to have to poll this function every couple of seconds, and you should assume the user moved the cursor if the time returned has decreased.

xnyhps
Polling that would be less efficient than just resetting a one-time timer when the mouse moves, though. A timer doesn't consume any CPU time except when it fires; the corollary to that is that a timer that fires only once uses less CPU time than a timer that fires repeatedly.
Peter Hosey
Thanks, this'll work great.
Ben Alpert