<!--
{
  "documentType" : "article",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/event-handling",
  "metadataVersion" : "0.1.0",
  "role" : "collectionGroup",
  "title" : "Event Handling"
}
-->

# Event Handling

Respond to mouse, keyboard, touch, and tablet events and gestures that originate inside your view.

## Topics

### Handling Events in the View

[`func acceptsFirstMouse(for: NSEvent?) -> Bool`](/documentation/AppKit/NSView/acceptsFirstMouse(for:))

Returns a Boolean value that indicates whether the view accepts the initial mouse-down event.

[`func hitTest(NSPoint) -> NSView?`](/documentation/AppKit/NSView/hitTest(_:))

Returns the farthest descendant of the view in the view hierarchy (including itself) that contains a specified point, or `nil` if that point lies completely outside the view.

[`func isMousePoint(NSPoint, in: NSRect) -> Bool`](/documentation/AppKit/NSView/isMousePoint(_:in:))

Returns whether a region of the view contains a specified point, accounting for whether the view is flipped or not.

[`func performKeyEquivalent(with: NSEvent) -> Bool`](/documentation/AppKit/NSView/performKeyEquivalent(with:))

Implemented by subclasses to respond to key equivalents (also known as keyboard shortcuts).

[`func rightMouseDown(with: NSEvent)`](/documentation/AppKit/NSResponder/rightMouseDown(with:))

Informs the receiver that the user has pressed the right mouse button.

[`var mouseDownCanMoveWindow: Bool`](/documentation/AppKit/NSView/mouseDownCanMoveWindow)

A Boolean value indicating whether the view can pass mouse down events through to its superviews.

[`var inputContext: NSTextInputContext?`](/documentation/AppKit/NSView/inputContext)

The text input context object for the view.

### Handling Touch Events

[`var allowedTouchTypes: NSTouch.TouchTypeMask`](/documentation/AppKit/NSView/allowedTouchTypes)

The types of touch interactions the view allows.

[`var wantsRestingTouches: Bool`](/documentation/AppKit/NSView/wantsRestingTouches)

A Boolean value indicating whether the view wants resting touches.

[`var candidateListTouchBarItem: NSCandidateListTouchBarItem<AnyObject>?`](/documentation/AppKit/NSView/candidateListTouchBarItem)

### Managing gesture recognizers

[`var gestureRecognizers: [NSGestureRecognizer]`](/documentation/AppKit/NSView/gestureRecognizers)

The gesture recognize objects currently attached to the view.

[`func addGestureRecognizer(NSGestureRecognizer)`](/documentation/AppKit/NSView/addGestureRecognizer(_:))

Attaches a gesture recognizer to the view.

[`func removeGestureRecognizer(NSGestureRecognizer)`](/documentation/AppKit/NSView/removeGestureRecognizer(_:))

Detaches a gesture recognizer from the view.

[`var exclusiveGestureBehavior: NSView.ExclusiveGestureBehavior`](/documentation/AppKit/NSView/exclusiveGestureBehavior-swift.property)

Declares whether gesture recognizers should be exclusive in this view and its subviews.

[`enum ExclusiveGestureBehavior`](/documentation/AppKit/NSView/ExclusiveGestureBehavior-swift.enum)

Exclusive gesture behavior

### Managing the Key-View Loop

[`var canBecomeKeyView: Bool`](/documentation/AppKit/NSView/canBecomeKeyView)

A Boolean value indicating whether the view can become key view.

[`var needsPanelToBecomeKey: Bool`](/documentation/AppKit/NSView/needsPanelToBecomeKey)

A Boolean value indicating whether the view needs its panel to become the key window before it can handle keyboard input and navigation.

[`var nextKeyView: NSView?`](/documentation/AppKit/NSView/nextKeyView)

The view object that follows the current view in the key view loop.

[`var nextValidKeyView: NSView?`](/documentation/AppKit/NSView/nextValidKeyView)

The closest view object in the key view loop that follows the current view in the key view loop and accepts first responder status.

[`var previousKeyView: NSView?`](/documentation/AppKit/NSView/previousKeyView)

The view object preceding the current view in the key view loop.

[`var previousValidKeyView: NSView?`](/documentation/AppKit/NSView/previousValidKeyView)

The closest view object in the key view loop that precedes the current view and accepts first responder status.

### Handling Smart Magnification

[`func rectForSmartMagnification(at: NSPoint, in: NSRect) -> NSRect`](/documentation/AppKit/NSView/rectForSmartMagnification(at:in:))

Returns the appropriate rectangle to use when magnifying around the specified point.

### Managing Tracking Areas

[`func addTrackingArea(NSTrackingArea)`](/documentation/AppKit/NSView/addTrackingArea(_:))

Adds a given tracking area to the view.

[`func removeTrackingArea(NSTrackingArea)`](/documentation/AppKit/NSView/removeTrackingArea(_:))

Removes a given tracking area from the view.

[`var trackingAreas: [NSTrackingArea]`](/documentation/AppKit/NSView/trackingAreas)

An array of the view’s tracking areas.

[`func updateTrackingAreas()`](/documentation/AppKit/NSView/updateTrackingAreas())

Invoked automatically when the view’s geometry changes such that its tracking areas need to be recalculated.

[`class let didUpdateTrackingAreasNotification: NSNotification.Name`](/documentation/AppKit/NSView/didUpdateTrackingAreasNotification)

Posted whenever a view recalculates its tracking areas.

### Managing Tracking Rectangles

[`func addTrackingRect(NSRect, owner: Any, userData: UnsafeMutableRawPointer?, assumeInside: Bool) -> NSView.TrackingRectTag`](/documentation/AppKit/NSView/addTrackingRect(_:owner:userData:assumeInside:))

Establishes an area for tracking mouse-entered and mouse-exited events within the view and returns a tag that identifies the tracking rectangle.

[`func removeTrackingRect(NSView.TrackingRectTag)`](/documentation/AppKit/NSView/removeTrackingRect(_:))

Removes the tracking rectangle identified by a tag.

[`typealias TrackingRectTag`](/documentation/AppKit/NSView/TrackingRectTag)

This type describes the rectangle used to track the mouse.

### Scrolling the View

[`func prepareContent(in: NSRect)`](/documentation/AppKit/NSView/prepareContent(in:))

Prepares the overdraw region for drawing.

[`var preparedContentRect: NSRect`](/documentation/AppKit/NSView/preparedContentRect)

The portion of the view that has been rendered and is available for responsive scrolling.

[`func scroll(NSPoint)`](/documentation/AppKit/NSView/scroll(_:))

Scrolls the view’s closest ancestor [`NSClipView`](/documentation/AppKit/NSClipView) object so a point in the view lies at the origin of the clip view’s bounds rectangle.

[`func scrollToVisible(NSRect) -> Bool`](/documentation/AppKit/NSView/scrollToVisible(_:))

Scrolls the view’s closest ancestor [`NSClipView`](/documentation/AppKit/NSClipView) object the minimum distance needed so a specified region of the view becomes visible in the clip view.

[`func autoscroll(with: NSEvent) -> Bool`](/documentation/AppKit/NSView/autoscroll(with:))

Scrolls the view’s closest ancestor [`NSClipView`](/documentation/AppKit/NSClipView) object proportionally to the distance of an event that occurs outside of it.

[`func adjustScroll(NSRect) -> NSRect`](/documentation/AppKit/NSView/adjustScroll(_:))

Overridden by subclasses to modify a given rectangle, returning the altered rectangle.

[`var enclosingScrollView: NSScrollView?`](/documentation/AppKit/NSView/enclosingScrollView)

The nearest ancestor scroll view that contains the current view.

[`func scroll(NSClipView, to: NSPoint)`](/documentation/AppKit/NSView/scroll(_:to:))

Notifies the superview of a clip view that the clip view needs to reset the origin of its bounds rectangle.

[`func reflectScrolledClipView(NSClipView)`](/documentation/AppKit/NSView/reflectScrolledClipView(_:))

Notifies a clip view’s superview that either the clip view’s bounds rectangle or the document view’s frame rectangle has changed, and that any indicators of the scroll position need to be adjusted.

[`class var isCompatibleWithResponsiveScrolling: Bool`](/documentation/AppKit/NSView/isCompatibleWithResponsiveScrolling)

A Boolean value that indicates whether views support responsive scrolling.

### Configuring Pressure

[`var pressureConfiguration: NSPressureConfiguration?`](/documentation/AppKit/NSView/pressureConfiguration)

Configures the behavior and progression of the Force Touch trackpad when responding to touch input produced by the user when the cursor is over the view.

### Dragging Operations

[`func registerForDraggedTypes([NSPasteboard.PasteboardType])`](/documentation/AppKit/NSView/registerForDraggedTypes(_:))

Registers the pasteboard types that the view will accept as the destination of an image-dragging session.

[`func unregisterDraggedTypes()`](/documentation/AppKit/NSView/unregisterDraggedTypes())

Unregisters the view as a possible destination in a dragging session.

[`var registeredDraggedTypes: [NSPasteboard.PasteboardType]`](/documentation/AppKit/NSView/registeredDraggedTypes)

The array of pasteboard drag types that the view can accept.

[`func beginDraggingSession(with: [NSDraggingItem], event: NSEvent, source: any NSDraggingSource) -> NSDraggingSession`](/documentation/AppKit/NSView/beginDraggingSession(with:event:source:))

Initiates a dragging session with a group of dragging items.

[`func beginDraggingSession(items: [NSDraggingItem], gesture: NSGestureRecognizer, source: any NSDraggingSource) -> NSDraggingSession?`](/documentation/AppKit/NSView/beginDraggingSession(items:gesture:source:))

Initiates a drag operation using a gesture recognizer.

[`func shouldDelayWindowOrdering(for: NSEvent) -> Bool`](/documentation/AppKit/NSView/shouldDelayWindowOrdering(for:))

Allows the user to drag objects from the view without activating the app or moving the window of the view forward, possibly obscuring the destination.



---

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)