iDevGames Forums
Hide Mouse Pointer - Printable Version

+- iDevGames Forums (
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Game Programming Fundamentals (/forum-7.html)
+--- Thread: Hide Mouse Pointer (/thread-4821.html)

Hide Mouse Pointer - JonTrainer - Nov 7, 2005 04:09 PM

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?


Hide Mouse Pointer - willThimbleby - Nov 7, 2005 04:16 PM

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.

Hide Mouse Pointer - seven - Nov 7, 2005 04:17 PM

If you haven't yet, download something like AppKiDo. It will help you find stuff, but anyway:

+ (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:

Hide Mouse Pointer - JonTrainer - Nov 7, 2005 05:05 PM

I tried +(void)hide but it hides the cursor over the whole screen.

I'll give the invisible cursor a try. Thanks.

Hide Mouse Pointer - Bachus - Nov 7, 2005 06:01 PM

Something along these lines:

- (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];

Hide Mouse Pointer - JonTrainer - Nov 7, 2005 08:18 PM

Bachus Wrote:
[[self window] setAcceptsMouseMovedEvents:YES];

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


Hide Mouse Pointer - TomorrowPlusX - Nov 8, 2005 08:30 AM

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.

Hide Mouse Pointer - Jordan - Nov 9, 2005 11:43 AM

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?

Hide Mouse Pointer - TomorrowPlusX - Nov 10, 2005 05:29 AM

I think you've misunderstanding how it works.

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

- (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 ).

- (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.

Hide Mouse Pointer - Jordan - Nov 10, 2005 10:46 AM

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