Re: mouseMoved: events (2 key points)
Re: mouseMoved: events (2 key points)
- Subject: Re: mouseMoved: events (2 key points)
- From: Mark Onyschuk <email@hidden>
- Date: Sun, 28 Mar 2004 12:50:17 -0500
There are two things you need to do to receive mouse moved events
inside a view:
1. You need to tell the window to accept mouse moved events with a
[[self window] setAcceptsMouseMovedEvents:YES] call
2. More importantly, you must ensure that your view is somewhere inside
the current responder chain in order to get a crack at the method! This
isn't an issue when your view is the first responder, or a superview of
it, but if you're a button, for example, somewhere on a window -
chances are you're not in the responder chain.
This second point is really the big issue, so what's to be done? Here's
a snippet of code I wrote (by coincidence, yesterday) that addresses
points 1 and 2:
- (void)mouseEntered:(NSEvent *)theEvent
{
oldAcceptsMouseMovedEvents = [[self window] acceptsMouseMovedEvents];
oldNextResponder = [[[self window] nextResponder] retain];
[[self window] setAcceptsMouseMovedEvents:YES];
[self setNextResponder:oldNextResponder];
[[self window] setNextResponder:self];
}
- (void)mouseExited:(NSEvent *)theEvent
{
[[self window] setAcceptsMouseMovedEvents: oldAcceptsMouseMovedEvents];
[[self window] setNextResponder:[oldNextResponder autorelease]];
[self setNextResponder:[self superview]];
[self setPageNumberHighlightFlags:APHighlightNone];
[self displayIfNeeded];
oldNextResponder = nil;
}
- (void)mouseMoved:(NSEvent *)theEvent
{
NSPoint location = [self convertPoint:[theEvent locationInWindow]
fromView:nil];
if (NSPointInRect(location, [self bounds])) {
if (location.x < NSMidX([self bounds])) {
[self setPageNumberHighlightFlags:APHighlightBackward];
[self displayIfNeeded];
} else {
[self setPageNumberHighlightFlags:APHighlightForward];
[self displayIfNeeded];
}
} else {
[self setPageNumberHighlightFlags:APHighlightNone];
}
}
I first set up a tracking rectangle on myself to receive
mouseEntered:/mouseExited: messages. Once I see a mouse entry, I turn
on mouseMoved messaging and install myself in between the window and
its own next responder (typically nil - the window is usually at the
end of its own responder chain). I choose this point in the responder
chain only because it's convenient, and can reverse my set-up in the
odd case that I'm removed from the window during tracking and more than
one nested view is using this scheme of tracking NSMouseMoved events...
Anyhow, that's one way to do it.
Mark
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.