---
title: "trackEvents(matching:timeout:mode:handler:)"
framework: appkit
role: symbol
role_heading: Instance Method
path: "appkit/nswindow/trackevents(matching:timeout:mode:handler:)"
---

# trackEvents(matching:timeout:mode:handler:)

Tracks events that match the specified mask using the specified tracking handler until the tracking handler explicitly terminates tracking.

## Declaration

```swift
func trackEvents(matching mask: NSEvent.EventTypeMask, timeout: TimeInterval, mode: RunLoop.Mode, handler trackingHandler: (NSEvent?, UnsafeMutablePointer<ObjCBool>) -> Void)
```

## Parameters

- `mask`: The event mask (see NSEventMask in doc://com.apple.appkit/documentation/AppKit/NSEvent for possible values).
- `timeout`: The maximum time interval the system waits for an event before passing nil to the handler.
- `mode`: The run loop mode.
- `trackingHandler`: A block that is called to track the events. The block takes the following parameters:

## Discussion

Discussion You can use this method in a tracking loop to get pressure events when you add pressure to the event mask. This method returns when tracking terminates. Each event is removed from the event queue and then passed to the tracking handler. If a matching event does not exist in the event queue, the main thread blocks in the specified runloop mode until an event of the requested type is received or the specified timeout expires. If the timeout expires, the tracking handler is called with a nil event (a negative timeout is interpreted as 0). Use NSEventDurationForever to prevent timing out. Tracking continues until you set stop to true. Note that calls to nextEvent(matching:) are allowed inside the trackingHandler block.

## See Also

### Handling Mouse Events

- [acceptsMouseMovedEvents](appkit/nswindow/acceptsmousemovedevents.md)
- [ignoresMouseEvents](appkit/nswindow/ignoresmouseevents.md)
- [mouseLocationOutsideOfEventStream](appkit/nswindow/mouselocationoutsideofeventstream.md)
- [windowNumber(at:belowWindowWithWindowNumber:)](appkit/nswindow/windownumber(at:belowwindowwithwindownumber:).md)
- [performDrag(with:)](appkit/nswindow/performdrag(with:).md)
- [foreverDuration](appkit/nsevent/foreverduration.md)
