<!--
{
  "availability" : [
    "macOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSTextAttachmentCellProtocol/trackMouse(with:in:of:untilMouseUp:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(pl)NSTextAttachmentCell(im)trackMouse:inRect:ofView:untilMouseUp:"
  },
  "title" : "trackMouse(with:in:of:untilMouseUp:)"
}
-->

# trackMouse(with:in:of:untilMouseUp:)

Handles a mouse-down event on the cell’s image, and optionally waits until a mouse-up event

```
@MainActor func trackMouse(with theEvent: NSEvent, in cellFrame: NSRect, of controlView: NSView?, untilMouseUp flag: Bool) -> Bool
```

## Parameters

`theEvent`

The mouse-down event.

`cellFrame`

The region of an [`NSTextView`](/documentation/AppKit/NSTextView) in which to track further mouse events.

`controlView`

The view that received the event. Typically, this view is an [`NSTextView`](/documentation/AppKit/NSTextView) object and is focused.

`flag`

A Boolean value that indicates whether to track the mouse until a mouse-up event occurs. If this parameter is <doc://com.apple.documentation/documentation/Swift/false>, stop tracking when a mouse-dragged event occurs outside of `cellFrame`.

## Return Value

<doc://com.apple.documentation/documentation/Swift/true> if the cell successfully finished tracking the mouse, or <doc://com.apple.documentation/documentation/Swift/false> if tracking was unsuccessful.

## Discussion

The [`NSTextAttachmentCell`](/documentation/AppKit/NSTextAttachmentCell-swift.class) implementation of this method calls upon `aTextView`’s delegate to handle the event. If `theEvent` is a mouse-up event for a double click, the text attachment cell calls the [`textView(_:doubleClickedOn:in:at:)`](/documentation/AppKit/NSTextViewDelegate/textView(_:doubleClickedOn:in:at:)) method of its delegate and returns <doc://com.apple.documentation/documentation/Swift/true>. Otherwise, depending on whether the user clicks or drags the cell, it sends the delegate a [`textView(_:clickedOn:in:at:)`](/documentation/AppKit/NSTextViewDelegate/textView(_:clickedOn:in:at:)): or a [`textView(_:draggedCell:in:event:at:)`](/documentation/AppKit/NSTextViewDelegate/textView(_:draggedCell:in:event:at:)) message and returns <doc://com.apple.documentation/documentation/Swift/true>. The [`NSTextAttachmentCell`](/documentation/AppKit/NSTextAttachmentCell-swift.class) implementation returns <doc://com.apple.documentation/documentation/Swift/false> only if `flag` is <doc://com.apple.documentation/documentation/Swift/false> and the mouse is dragged outside of `cellFrame`. The delegate methods are invoked only if the delegate responds.

## See Also

[`trackMouse(with:in:of:untilMouseUp:)`](/documentation/AppKit/NSCell/trackMouse(with:in:of:untilMouseUp:))

Initiates the mouse tracking behavior in a cell.

[`lockFocus()`](/documentation/AppKit/NSView/lockFocus())

Locks the focus on the view, so subsequent commands take effect in the view’s window and coordinate system.



---

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)