Hide Mouse Pointer
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.
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.
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.
If you haven't yet, download something like AppKiDo. It will help you find stuff, but anyway:
Quote: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.Code:
+ (void)hide
hide overrides setHiddenUntilMouseMoves:
Jericho
I tried +(void)hide but it hides the cursor over the whole screen.
I'll give the invisible cursor a try. Thanks.
I'll give the invisible cursor a try. Thanks.
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];
}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!
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.
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?
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?
I think you've misunderstanding how it works.
Your NSView ( or NSOpenGLView ) subclass has to have -- in it's @implementation -- the method:
And, somewhere in an awakeFromNib, you've got to call something like this ( in this case, it's in the NSView suclass's implementation ).
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.
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.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| memory pointer tricks? | Toontingy | 2 | 3,676 |
Mar 31, 2009 02:35 AM Last Post: Ingemar |
|
| pointer cleanup questions | kendric | 7 | 3,854 |
Mar 29, 2009 07:48 PM Last Post: kendric |
|
| pointer or not? | kensuguro | 17 | 7,224 |
Aug 15, 2007 04:12 PM Last Post: AnotherJake |
|
| My AnimationManager didn't like being a pointer. | milkfilk | 6 | 3,454 |
Mar 24, 2007 10:03 PM Last Post: unknown |
|
| Pointer-related woes (C/C++) | sealfin | 2 | 2,294 |
Jan 18, 2006 01:22 PM Last Post: sealfin |
|

