View Full Version : Cocoa: mouseMoved
XxtraLarGe
2004.01.26, 12:28 PM
How do I get my view to recognize a mouseMoved event? I have the following code in an NSView subclass:
- (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:
- (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. :(
XxtraLarGe
2004.01.26, 04:58 PM
It's - (void)setAcceptsMouseMovedEvents:(BOOL)flag , but that doesn't seem to do anything either. I tried:
[[self window] setAcceptsMouseMovedEvents:YES];
Why should something so basic be so difficult to find out?
XxtraLarGe
2004.01.26, 09:53 PM
O.k., I figured it out. Two simple lines in my awakeFromNib method:
[[self window] makeFirstResponder:self];
[[self window] setAcceptsMouseMovedEvents:YES];
GoodDoug
2004.01.27, 02:52 PM
O.k., I figured it out. Two simple lines in my awakeFromNib method:
[[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.
djohnson
2004.01.27, 05:24 PM
I can say that this helped me out a lot! I tried each thing by itself, but never put them together. Thanks!!! :D
XxtraLarGe
2004.01.28, 01:16 AM
I can say that this helped me out a lot! I tried each thing by itself, but never put them together. Thanks!!! :D
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.
djohnson
2004.01.28, 12:46 PM
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:
BeyondCloister
2004.01.28, 01:08 PM
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:
djohnson
2004.01.28, 11:30 PM
That would be a book I would buy almost immediately. Unfortunately, the book would probably be really bad. :D
XxtraLarGe
2004.01.28, 11:53 PM
That would be a book I would buy almost immediately. Unfortunately, the book would probably be really bad. :D
You know what that means? I'd be the perfect person to write it! :lol:
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? ;)
djohnson
2004.01.29, 12:24 PM
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. :D
gefhowie
2008.10.04, 03:52 PM
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];
}
longjumper
2008.10.08, 06:55 PM
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.
vBulletin® v3.6.8, Copyright ©2000-2008, Jelsoft Enterprises Ltd.