Cocoa: mouseMoved
How do I get my view to recognize a mouseMoved event? I have the following code in an NSView subclass:
mouseDown & mouseDragged work perfectly.
I tried adding the following code to my window's delegate:
It doesn't do anything. Any hints?
Code:
- (void)mouseMoved:(NSEvent *)event {
NSPoint eventLocation = [event locationInWindow];
center = [self convertPoint:eventLocation fromView:nil];
[self setNeedsDisplay:YES];
}mouseDown & mouseDragged work perfectly.
I tried adding the following code to my window's delegate:
Code:
- (BOOL)acceptsMouseMovedEvents {
return YES;
}It doesn't do anything. Any hints?
If I recall, you have to use some set method to receive mouseMoved events. I believe I actually asked this question quite a while ago, so you may try doing a search. Unfortunately, all my source is not on this machine so I can't find the exact method name right now.
It's - (void)setAcceptsMouseMovedEvents:(BOOL)flag , but that doesn't seem to do anything either. I tried:
Why should something so basic be so difficult to find out?
Code:
[[self window] setAcceptsMouseMovedEvents:YES];Why should something so basic be so difficult to find out?
O.k., I figured it out. Two simple lines in my awakeFromNib method:
Code:
[[self window] makeFirstResponder:self];
[[self window] setAcceptsMouseMovedEvents:YES];XxtraLarGe Wrote:O.k., I figured it out. Two simple lines in my awakeFromNib method:
Code:
[[self window] makeFirstResponder:self];
[[self window] setAcceptsMouseMovedEvents:YES];
This should go in the FAQ when it gets back up. This stumps everyone the first time.
I can say that this helped me out a lot! I tried each thing by itself, but never put them together. Thanks!!!
djohnson Wrote:I can say that this helped me out a lot! I tried each thing by itself, but never put them together. Thanks!!!
Glad it was helpful. It is very simple, but it's very cryptic to figure out. It would be nice if Apple's docs included "useage" along with the method descriptions.
Apple's docs are very dry. You almost need to know what they mean before you read them! It would be nice if they included some more tutorials, maybe ones for the average programmer instead of the Apple employee?
djohnson Wrote:Apple's docs are very dry. You almost need to know what they mean before you read them! It would be nice if they included some more tutorials, maybe ones for the average programmer instead of the Apple employee?
What the world really needs is some kind of Cocoa book where all the examples are based around game development. Useful examples that help build to something fun rather than yet another currency converter.
That would be a book I would buy almost immediately. Unfortunately, the book would probably be really bad.
djohnson Wrote:That would be a book I would buy almost immediately. Unfortunately, the book would probably be really bad.
You know what that means? I'd be the perfect person to write it!
BeyondCloister Wrote:What the world really needs is some kind of Cocoa book where all the examples are based around game development. Useful examples that help build to something fun rather than yet another currency converter.Maybe relating to board games or something?
Ahh but maybe Apple thinks we can just use an OpenGL book and maybe a game design book? It is the same basic principal for Mac or windoze.
I needed an extra call - setInitialFirstResponder. Don't know if this can be set in interface builder instead of in code but I didn't get mouseMoved: events without it.
- (void)awakeFromNib
{
[[self window] setInitialFirstResponder:self];
[[self window] makeFirstResponder:self];
[[self window] setAcceptsMouseMovedEvents:YES];
}
- (void)awakeFromNib
{
[[self window] setInitialFirstResponder:self];
[[self window] makeFirstResponder:self];
[[self window] setAcceptsMouseMovedEvents:YES];
}
Mouse moved events are only sent to the first responder. A call to setFirstResponder in awakeFromNib in a view instance will set the first responder of the receiver to that view, but the order in which your objects in a nib file are sent awakeFromNib aren't guaranteed. The first responder of the window could be overwritten after that awakeFromNib call.
You can set up an application delegate to put the final touches on your responder chain when it finishes launching. I'm not sure if you can set the initial first responder of a window from IB, it's been awhile... but my guess would be yes, you can.
You can set up an application delegate to put the final touches on your responder chain when it finishes launching. I'm not sure if you can set the initial first responder of a window from IB, it's been awhile... but my guess would be yes, you can.

