0
votes

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.

1

1 Answers

1
votes

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.