Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7248965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:12:58+00:00 2026-05-28T22:12:58+00:00

Why mouseExited/mouseEntered isn’t called when mouse exits from NStrackingArea by scrolling or doing animation?

  • 0

Why mouseExited/mouseEntered isn’t called when mouse exits from NStrackingArea by scrolling or doing animation?

I create code like this:

Mouse entered and exited:

-(void)mouseEntered:(NSEvent *)theEvent {
    NSLog(@"Mouse entered");
}

-(void)mouseExited:(NSEvent *)theEvent
{
    NSLog(@"Mouse exited");
}

Tracking area:

-(void)updateTrackingAreas
{ 
    if(trackingArea != nil) {
        [self removeTrackingArea:trackingArea];
        [trackingArea release];
    }

    int opts = (NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways);
    trackingArea = [ [NSTrackingArea alloc] initWithRect:[self bounds]
                                             options:opts
                                               owner:self
                                            userInfo:nil];
    [self addTrackingArea:trackingArea];
}

More details:

I have added NSViews as subviews in NSScrollView’s view. Each NSView have his own tracking area and when I scroll my scrollView and leave tracking area “mouseExited” isn’t called but without scrolling everything works fine. Problem is that when I scroll “updateTrackingAreas” is called and I think this makes problems.

* Same problem with just NSView without adding it as subview so that’s not a problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T22:12:58+00:00Added an answer on May 28, 2026 at 10:12 pm

    As you noted in the title of the question, mouseEntered and mouseExited are only called when the mouse moves. To see why this is the case, let’s first look at the process of adding NSTrackingAreas for the first time.

    As a simple example, let’s create a view that normally draws a white background, but if the user hovers over the view, it draws a red background. This example uses ARC.

    @interface ExampleView
    
    - (void) createTrackingArea
    
    @property (nonatomic, retain) backgroundColor;
    @property (nonatomic, retain) trackingArea;
    
    @end
    
    @implementation ExampleView
    
    @synthesize backgroundColor;
    @synthesize trackingArea
    
    - (id) awakeFromNib
    {
        [self setBackgroundColor: [NSColor whiteColor]];
        [self createTrackingArea];
    }
    
    - (void) createTrackingArea
    {
        int opts = (NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways);
        trackingArea = [ [NSTrackingArea alloc] initWithRect:[self bounds]
                                                 options:opts
                                                   owner:self
                                                userInfo:nil];
        [self addTrackingArea:trackingArea];
    }
    
    - (void) drawRect: (NSRect) rect
    {
        [[self backgroundColor] set];
        NSRectFill(rect);
    }
    
    - (void) mouseEntered: (NSEvent*) theEvent
    {
        [self setBackgroundColor: [NSColor redColor]];
    }
    
    - (void) mouseEntered: (NSEvent*) theEvent
    {
        [self setBackgroundColor: [NSColor whiteColor]];
    }
    
    @end
    

    There are two problems with this code. First, when -awakeFromNib is called, if the mouse is already inside the view, -mouseEntered is not called. This means that the background will still be white, even though the mouse is over the view. This is actually mentioned in the NSView documentation for the assumeInside parameter of -addTrackingRect:owner:userData:assumeInside:

    If YES, the first event will be generated when the cursor leaves aRect, regardless if the cursor is inside aRect when the tracking rectangle is added. If NO the first event will be generated when the cursor leaves aRect if the cursor is initially inside aRect, or when the cursor enters aRect if the cursor is initially outside aRect.

    In both cases, if the mouse is inside the tracking area, no events will be generated until the mouse leaves the tracking area.

    So to fix this, when we add the tracking area, we need to find out if the cursor is within in the tracking area. Our -createTrackingArea method thus becomes

    - (void) createTrackingArea
    {
        int opts = (NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways);
        trackingArea = [ [NSTrackingArea alloc] initWithRect:[self bounds]
                                                 options:opts
                                                   owner:self
                                                userInfo:nil];
        [self addTrackingArea:trackingArea];
    
        NSPoint mouseLocation = [[self window] mouseLocationOutsideOfEventStream];
        mouseLocation = [self convertPoint: mouseLocation
                                  fromView: nil];
    
        if (NSPointInRect(mouseLocation, [self bounds]))
        {
            [self mouseEntered: nil];
        }
        else
        {
            [self mouseExited: nil];
        }
    }
    

    The second problem is scrolling. When scrolling or moving a view, we need to recalculate the NSTrackingAreas in that view. This is done by removing the tracking areas and then adding them back in. As you noted, -updateTrackingAreas is called when you scroll the view. This is the place to remove and re-add the area.

    - (void) updateTrackingAreas
    {
        [self removeTrackingArea:trackingArea];
        [self createTrackingArea];
        [super updateTrackingAreas]; // Needed, according to the NSView documentation
    }
    

    And that should take care of your problem. Admittedly, needing to find the mouse location and then convert it to view coordinates every time you add a tracking area is something that gets old quickly, so I would recommend creating a category on NSView that handles this automatically. You won’t always be able to call [self mouseEntered: nil] or [self mouseExited: nil], so you might want to make the category accept a couple blocks. One to run if the mouse is in the NSTrackingArea, and one to run if it is not.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bunch of JLabels and i would like to trap mouse click
I've got a problem here. I'm creating a NSTrackingArea like this: NSTrackingArea *area =
So I wrote this code in java. It should print Mouse Click when I
I'm practicing writing MVC applications. I have a Mastermind game, that I would like
I have create a popup menu and on right click in a panel the
OK so I'm doing a small part of my inventory. I got MOST of
OK so I switched from JList to List because 1.) It doesn't overlap my
Is mouseMotionListener going to trigger an event once the mouse moves over the component,
I would like to convert my Simple Java program to an Applet Program. I've
Basically, I am trying to create an app, that displays images. filename variable is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.