Hide Cursor Again?

Discussion in 'Mac Programming' started by Darkroom, Jul 5, 2008.

  1. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #1
    the following code will hide the mouse pointer once until the mouse is moved... the problem is that once it's moved, it doesn't disappear again if the view is the same... what method should i call to make it disappear again after say 2 seconds if the view hasn't changed?

    Code:
    -(void)hideMouseOnFullScreenView
    	{
    	if ([FullScreenView isVisible])
    		{
    		[NSCursor setHiddenUntilMouseMoves:YES];
    		}
    	}
    
     
  2. kpua macrumors 6502

    Joined:
    Jul 25, 2006
    #2
    How about just listen for mouseMoved events in your view. Every time the mouse is moved, reset a timer to 2 seconds and make the timer call your -hideMouse... method upon firing.
     
  3. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #3
    there's something wrong with my code... totally doing something dumb for sure... coffee is on, but my logic hasn't kicked in yet... so, in my view class i've got this.

    Code:
    - (void)awakeFromNib
    	{
    	[self setAcceptsMouseMovedEvents:YES];
    	}
    
    -(void)mouseMoved:(NSEvent *)event
    	{
    	if (timer == nil)
    		{
    		NSLog(@"Timer Started / Mouse Moved");
    		timer = [[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(hideMouse:) userInfo:nil repeats:NO] retain];
    		}
    		else
    		{
    		NSLog(@"Timer Ended");
    		[timer invalidate];
    		[timer release];
    		timer = nil;
    		}
            }
    
    -(void)hideMouse:(NSTimer *)timer
    	{
    	[NSCursor setHiddenUntilMouseMoves:YES];
    	}
    
     
  4. yoavcs macrumors regular

    Joined:
    Apr 7, 2004
    Location:
    Israel
    #4
    Your timer is never being allowed to fire. On every mouse move you are reseting it back to 2.0 seconds. Take another look at your timer setting if statement in mouseMoved:
     
  5. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #5
    this seems to work:

    Code:
    - (void)awakeFromNib
    	{
    	[self setAcceptsMouseMovedEvents:YES];
    	}
    
    -(void)mouseMoved:(NSEvent *)theEvent
    	{
    	NSLog (@"Timer Reset");
    	[timer invalidate];
    	
    	NSLog(@"Timer Started");
    	timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(hideMouse:) userInfo:nil repeats:NO];
    	}
    
    -(void)hideMouse:(NSTimer *)theTimer
    	{
    	[NSCursor setHiddenUntilMouseMoves:YES];
    	timer = nil;
    	}
    
    hope my new code isn't dangerous, although i believe it's fine... if anyone can comment on it's safety that would be great...
     

Share This Page