Hide Mouse Pointer

Apprentice
Posts: 18
Joined: 2005.07
Post: #1
I have a window with a custom NSView.

I would like to hide the mouse pointer while it is over the window, and show it when it is outside the window bounds.

I'm having trouble finding how to do this. Anyone know?

Thanks.
Quote this message in a reply
Member
Posts: 78
Joined: 2002.06
Post: #2
You could always make an invisible nscursor from a blank image and set it to be active over the view. Something like addCursorRect: (NSRect)aRect cursor: (NSCursor *)aCursor on your custom view.
Quote this message in a reply
Member
Posts: 40
Joined: 2004.12
Post: #3
If you haven't yet, download something like AppKiDo. It will help you find stuff, but anyway:

Quote:hide
Code:
+ (void)hide
Makes the current cursor invisible. If another cursor becomes current, that cursor will be invisible, too. It will remain invisible until you invoke the unhide method.
hide overrides setHiddenUntilMouseMoves:

Jericho
Quote this message in a reply
Apprentice
Posts: 18
Joined: 2005.07
Post: #4
I tried +(void)hide but it hides the cursor over the whole screen.


I'll give the invisible cursor a try. Thanks.
Quote this message in a reply
Member
Posts: 269
Joined: 2005.04
Post: #5
Something along these lines:

Code:
- (void) awakeFromNib
{
    [[self window] makeFirstResponder:self];
    [[self window] setAcceptsMouseMovedEvents:YES];
}

- (void) mouseMoved:(NSEvent *)theEvent
{
    NSPoint     mouseLoc = [[self window] convertScreenToBase:[NSEvent mouseLocation]];
    
    [NSCursor hide];
    
    if (mouseLoc.x < 0)
        [NSCursor unhide];
    else if (mouseLoc.x > [[self window] frame].size.width)
        [NSCursor unhide];
    
    if (mouseLoc.y < 0)
        [NSCursor unhide];
    else if (mouseLoc.y > [[self window] frame].size.height)
        [NSCursor unhide];
}
Quote this message in a reply
Apprentice
Posts: 18
Joined: 2005.07
Post: #6
Bachus Wrote:
Code:
[[self window] setAcceptsMouseMovedEvents:YES];

Ah! That's what I was missing. I couldn't figure out how to get the mouseMoved event to fire.

Thanks!
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #7
A warning about hiding, but not disabling, the mouse pointer: it can be annoying if you're using the mouse pointer for game control if you don't lock it to window bounds since moving the mouse and clicking will eventually cause the app to lose focus, since the mouse click will be *outside* the app.
Quote this message in a reply
Jordan
Unregistered
 
Post: #8
I've never been able to get a Cocoa application to accept mouse moved events.

Does location matter for where the -setAcceptsMouseMovedEvents is placed? Does it have to be in -(void)awakeFromNib or can I place it anywhere in my program allowing me to turn it on and off throughout execution of the program?
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #9
I think you've misunderstanding how it works.

Your NSView ( or NSOpenGLView ) subclass has to have -- in it's @implementation -- the method:

Code:
- (BOOL) acceptsFirstResponder
{
    return YES;
}

And, somewhere in an awakeFromNib, you've got to call something like this ( in this case, it's in the NSView suclass's implementation ).

Code:
- (void) awakeFromNIB
{
    /*
        This has to be done to get keypresses
    */
    [[self window] makeFirstResponder: self];
}

Now, your view will get mouse moved events. If you're dealing with game-like keyboard and mouse handling, it's more complicated, since you'll have to override your app's event processing loop.
Quote this message in a reply
Jordan
Unregistered
 
Post: #10
I'll give that a try.

I had found away around it. I set up a timer. Every time the timer fired the method invoked would get the mouse position with

-(NSPoint)mouseLocationOutsideOfEventStream.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  memory pointer tricks? Toontingy 2 4,247 Mar 31, 2009 02:35 AM
Last Post: Ingemar
  pointer cleanup questions kendric 7 4,696 Mar 29, 2009 07:48 PM
Last Post: kendric
  pointer or not? kensuguro 17 7,944 Aug 15, 2007 04:12 PM
Last Post: AnotherJake
  My AnimationManager didn't like being a pointer. milkfilk 6 3,995 Mar 24, 2007 10:03 PM
Last Post: unknown
  Pointer-related woes (C/C++) sealfin 2 2,745 Jan 18, 2006 01:22 PM
Last Post: sealfin