<!--
{
  "availability" : [
    "macOS: 10.5.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSTrackingArea",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSTrackingArea"
  },
  "title" : "NSTrackingArea"
}
-->

# NSTrackingArea

A region of a view that generates mouse-tracking and cursor-update events when the pointer is over that region.

```
class NSTrackingArea
```

## Overview

When creating a tracking-area object, you specify a rectangle (in the view’s coordinate system), an owning object, and one or more options, along with (optionally) a dictionary of data. After it’s created, you add the tracking-area object to a view using the [`addTrackingArea(_:)`](/documentation/AppKit/NSView/addTrackingArea(_:)) method. Depending on the options specified, the owner of the tracking area receives [`mouseEntered(with:)`](/documentation/AppKit/NSResponder/mouseEntered(with:)), [`mouseExited(with:)`](/documentation/AppKit/NSResponder/mouseExited(with:)), [`mouseMoved(with:)`](/documentation/AppKit/NSResponder/mouseMoved(with:)), and [`cursorUpdate(with:)`](/documentation/AppKit/NSResponder/cursorUpdate(with:)) messages when the mouse cursor enters, moves within, and leaves the tracking area. Currently the tracking area is restricted to rectangles.

An [`NSTrackingArea`](/documentation/AppKit/NSTrackingArea) object belongs to its view rather than to its window. Consequently, you can add and remove tracking rectangles without needing to worry if the view has been added to a window. In addition, this design makes it possible for the AppKit to compute the geometry of tracking areas automatically when a view moves and, in some cases, when a view changes size.

Using [`NSTrackingArea`](/documentation/AppKit/NSTrackingArea), you can configure the scope of activity for mouse tracking. There are four options:

- The tracking area is active only when the view is first responder.
- The tracking area is active when the view is in the key window.
- The tracking area is active when the application is active.
- The tracking area is active always (even when the application is inactive).

Other options for [`NSTrackingArea`](/documentation/AppKit/NSTrackingArea) objects include specifying that the tracking area should be synchronized with the visible rectangle of the view ([`visibleRect`](/documentation/AppKit/NSView/visibleRect)) and for generating `mouseEntered:` and `mouseExited`: events when the mouse is dragged.

Other [`NSView`](/documentation/AppKit/NSView) methods related to [`NSTrackingArea`](/documentation/AppKit/NSTrackingArea) objects (in addition to [`addTrackingArea(_:)`](/documentation/AppKit/NSView/addTrackingArea(_:))) include [`removeTrackingArea(_:)`](/documentation/AppKit/NSView/removeTrackingArea(_:)) and [`updateTrackingAreas()`](/documentation/AppKit/NSView/updateTrackingAreas()). Views can override the latter method to recompute and replace their [`NSTrackingArea`](/documentation/AppKit/NSTrackingArea) objects in certain situations, such as a change in the size of the `visibleRect`.

## Topics

### Initializing the Tracking-Area Object

[`-  initWithRect:options:owner:userInfo:`](/documentation/AppKit/NSTrackingArea/init(rect:options:owner:userInfo:))

Initializes and returns an object defining a region of a view to receive mouse-tracking events, mouse-moved events, cursor-update events, or possibly all these events.

### Getting Object Attributes

[`options`](/documentation/AppKit/NSTrackingArea/options-swift.property)

The options specified for the receiver.

[`owner`](/documentation/AppKit/NSTrackingArea/owner)

The object owning the receiver, which is the recipient of mouse-tracking, mouse-movement, and cursor-update messages.

[`rect`](/documentation/AppKit/NSTrackingArea/rect)

The rectangle defining the area encompassed by the receiver.

[`userInfo`](/documentation/AppKit/NSTrackingArea/userInfo)

The dictionary containing the data associated with the receiver when it was created.

### Constants

[`Options`](/documentation/AppKit/NSTrackingArea/Options-swift.struct)

The data type defined for the constants specified in the `options` parameter of [`init(rect:options:owner:userInfo:)`](/documentation/AppKit/NSTrackingArea/init(rect:options:owner:userInfo:)). These constants are described below; you can specify multiple constants by performing a bitwise-OR operation with them. In particular, you must supply one or more of the tracking-type constants (that is, [`mouseEnteredAndExited`](/documentation/AppKit/NSTrackingArea/Options-swift.struct/mouseEnteredAndExited), [`mouseMoved`](/documentation/AppKit/NSTrackingArea/Options-swift.struct/mouseMoved), and [`cursorUpdate`](/documentation/AppKit/NSTrackingArea/Options-swift.struct/cursorUpdate)) and one of the active constants (that is, [`activeWhenFirstResponder`](/documentation/AppKit/NSTrackingArea/Options-swift.struct/activeWhenFirstResponder), [`activeInKeyWindow`](/documentation/AppKit/NSTrackingArea/Options-swift.struct/activeInKeyWindow), [`activeInActiveApp`](/documentation/AppKit/NSTrackingArea/Options-swift.struct/activeInActiveApp), and [`activeAlways`](/documentation/AppKit/NSTrackingArea/Options-swift.struct/activeAlways)). In addition, you may specify any of the behavior constants (that is, [`assumeInside`](/documentation/AppKit/NSTrackingArea/Options-swift.struct/assumeInside), [`inVisibleRect`](/documentation/AppKit/NSTrackingArea/Options-swift.struct/inVisibleRect), and [`enabledDuringMouseDrag`](/documentation/AppKit/NSTrackingArea/Options-swift.struct/enabledDuringMouseDrag)).

## Relationships

### Conforms To

[`Hashable`](/documentation/Swift/Hashable)

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`CVarArg`](/documentation/Swift/CVarArg)

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

[`Equatable`](/documentation/Swift/Equatable)

[`NSCoding`](/documentation/Foundation/NSCoding)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

[`NSCopying`](/documentation/Foundation/NSCopying)

### Inherits From

[`NSObject-swift.class`](/documentation/ObjectiveC/NSObject-swift.class)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)