views:

210

answers:

1

At first I had one window with my custom control. To get it to accept the mouse moved events I simply put in it's awakeFromNib:

Code:

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

Now I'm doing something with four of them in the same window, and this doesn't work so pretty anymore. First off, I took them out of the control's awakeFromNib and decided I'd use my appController to manage it i.e. [window makeFirstResponder:View]

My question is, how do I manage four of these in the same view if I want each one to respond to mouse moved events? Right now, I've told the window to respond to mouseMoved events but none of the views are responding to mouseMoved.

A: 

You will also need to override -acceptsFirstResponder to return YES.

    #pragma mark NSResponder Overrides
    - (BOOL)acceptsFirstResponder
    {
    return YES;
    }

-mouseMoved events are expensive so I turn off mouse moved events when my control's -mouseExited message is called and I turn it on in -mouseEntered.

    - (void)mouseEntered:(NSEvent *)theEvent
{
    [[self window] setAcceptsMouseMovedEvents:YES];
    [[self window] makeFirstResponder:self];
}

- (void)mouseMoved:(NSEvent *)theEvent
{
...
}

- (void)mouseExited:(NSEvent *)theEvent
{
    [[self window] setAcceptsMouseMovedEvents:NO];
}

I quickly tested this in my custom control application. I duplicated the control several times in the nib file and it worked as expected.

You may also need:

- (void)awakeFromNib
{
    [[self window] setAcceptsMouseMovedEvents:YES];
    [self addTrackingRect:[self bounds] owner:self userData:NULL assumeInside:YES];
}

I don't think the -setAcceptsMouseMovedEvents is necessary, but I'm pretty sure the tracking rect code is. You may also need to experiment with the value of the assumeInside: parameter, but that is documented.

Mark Thalman
I added mouseEntered: and mouseExited: and implemented them as above, but it's not calling the methods (NSLog within is not displaying). Also, what does #pragma mark NSResponder Overrides mean
I updated the answer for a bit that I forgot. The '#pragma mark' is a directive that XCode parses and adds an item to the function popup menu to help find different places in your code. It is completely ignored by the compiler.Using a dash(-) as the string will put a separator item in the menu.
Mark Thalman
thanks alot that worked fine :D

related questions