I have a NSWindow that I’m putting into fullscreen mode. I would like to be able to hide the mouse when its not in use (say 15 seconds after it was last used). I have my application delegate as follows:
MyMediaRoomAppDelegate.h:
#import <Cocoa/Cocoa.h>
@interface MyMediaRoomAppDelegate : NSResponder <NSApplicationDelegate> {
NSWindow *window;
NSDate *lastMouseMove;
}
@property (assign) IBOutlet NSWindow *window;
@property (nonatomic, retain) NSDate *lastMouseMove;
@end
MyMediaRoomAppDelegate.m:
#import "MyMediaRoomAppDelegate.h"
@implementation MyMediaRoomAppDelegate
@synthesize window;
@synthesize lastMouseMove;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// The application has just finished lanching
// Grab the screen size
NSRect screenRect;
screenRect = [[NSScreen mainScreen] frame];
// Setup the window - full screen
[[self window] setLevel:NSMainMenuWindowLevel+1];
[[self window] setStyleMask:NSBorderlessWindowMask];
[[self window] setOpaque:YES];
[[self window] setBackgroundColor:[NSColor blackColor]];
[[self window] setFrame:screenRect display:YES animate:NO];
// Setup the mouse
[[self window] setAcceptsMouseMovedEvents:YES];
[[self window] makeFirstResponder:self];
[NSCursor hide];
}
- (BOOL)acceptsFirstResponder
{
return YES;
}
- (void)mouseMoved:(NSEvent *)theEvent
{
[NSCursor unhide];
[self setLastMouseMove: [NSDate date]];
}
@end
What I’m not sure about is how to re-hide the cursor after 15 seconds. The thing is I need to check setLastMouseMove, every second or so, not just call [NSCursor hide] after 15 seconds is up.
Try NSTimer. You can specify it to be repeating at creation time.