Cocoa: mouseMoved

Apprentice
Posts: 14
Joined: 2008.10
Post: #1
How do I get my view to recognize a mouseMoved event? I have the following code in an NSView subclass:

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?
Quote this message in a reply
Moderator
Posts: 608
Joined: 2002.04
Post: #2
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. Sad
Quote this message in a reply
Apprentice
Posts: 14
Joined: 2008.10
Post: #3
It's - (void)setAcceptsMouseMovedEvents:(BOOL)flag , but that doesn't seem to do anything either. I tried:

Code:
[[self window] setAcceptsMouseMovedEvents:YES];

Why should something so basic be so difficult to find out?
Quote this message in a reply
Apprentice
Posts: 14
Joined: 2008.10
Post: #4
O.k., I figured it out. Two simple lines in my awakeFromNib method:

Code:
[[self window] makeFirstResponder:self];
    [[self window] setAcceptsMouseMovedEvents:YES];
Quote this message in a reply
Member
Posts: 104
Joined: 2002.04
Post: #5
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.
Quote this message in a reply
djohnson
Unregistered
 
Post: #6
I can say that this helped me out a lot! I tried each thing by itself, but never put them together. Thanks!!! Grin
Quote this message in a reply
Apprentice
Posts: 14
Joined: 2008.10
Post: #7
djohnson Wrote:I can say that this helped me out a lot! I tried each thing by itself, but never put them together. Thanks!!! Grin

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.
Quote this message in a reply
djohnson
Unregistered
 
Post: #8
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? Blink
Quote this message in a reply
Moderator
Posts: 592
Joined: 2002.12
Post: #9
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? Blink

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. Sneaky
Quote this message in a reply
djohnson
Unregistered
 
Post: #10
That would be a book I would buy almost immediately. Unfortunately, the book would probably be really bad. Grin
Quote this message in a reply
Apprentice
Posts: 14
Joined: 2008.10
Post: #11
djohnson Wrote:That would be a book I would buy almost immediately. Unfortunately, the book would probably be really bad. Grin

You know what that means? I'd be the perfect person to write it! LOL
Quote this message in a reply
Moderator
Posts: 608
Joined: 2002.04
Post: #12
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. Sneaky
Maybe relating to board games or something? Wink
Quote this message in a reply
djohnson
Unregistered
 
Post: #13
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. Grin
Quote this message in a reply
Nibbie
Posts: 1
Joined: 2008.10
Post: #14
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];
}
Quote this message in a reply
Moderator
Posts: 133
Joined: 2008.05
Post: #15
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.
Quote this message in a reply
Post Reply