---
title: "discardEvents(matching:before:)"
framework: appkit
role: symbol
role_heading: Instance Method
path: "appkit/nsapplication/discardevents(matching:before:)"
---

# discardEvents(matching:before:)

Removes all events matching the given mask and generated before the specified event.

## Declaration

```swift
func discardEvents(matching mask: NSEvent.EventTypeMask, before lastEvent: NSEvent?)
```

## Parameters

- `mask`: Contains one or more flags indicating the types of events to discard. The constants section of the doc://com.apple.appkit/documentation/AppKit/NSEvent class defines the constants you can add together to create this mask. The discussion section also lists some of the constants that are typically used.
- `lastEvent`: A marker event that you use to indicate which events should be discarded. Events that occurred before this event are discarded but those that occurred after it are not.

## Discussion

Discussion Use this method to ignore any events that occurred before a specific event. For example, suppose your app has a tracking loop that you exit when the user releases the mouse button. You could use this method, specifying NSAnyEventMask as the mask argument and the ending mouse-up event as the lastEvent argument, to discard all events that occurred while you were tracking mouse movements in your loop. Passing the mouse-up event as lastEvent ensures that any events that might have occurred after the mouse-up event (that is, that appear in the queue after the mouse-up event) aren’t discarded. note: Typically, you send this message to an NSWindow object, rather than to the app object. Discarding events for a window clears out all of the events for that window only, leaving events for other windows in place. For the mask parameter, you can add together event type constants such as the following: NSLeftMouseDownMask NSLeftMouseUpMask NSRightMouseDownMask NSRightMouseUpMask NSMouseMovedMask NSLeftMouseDraggedMask NSRightMouseDraggedMask NSMouseEnteredMask NSMouseExitedMask NSKeyDownMask NSKeyUpMask NSFlagsChangedMask NSPeriodicMask NSCursorUpdateMask NSAnyEventMask This method can also be called in subthreads. Events posted in subthreads bubble up in the main thread event queue.

## See Also

### Managing the event loop

- [nextEvent(matching:until:inMode:dequeue:)](appkit/nsapplication/nextevent(matching:until:inmode:dequeue:).md)
- [currentEvent](appkit/nsapplication/currentevent.md)
- [isRunning](appkit/nsapplication/isrunning.md)
- [run()](appkit/nsapplication/run().md)
- [finishLaunching()](appkit/nsapplication/finishlaunching().md)
- [stop(_:)](appkit/nsapplication/stop(_:).md)
- [sendEvent(_:)](appkit/nsapplication/sendevent(_:).md)
- [postEvent(_:atStart:)](appkit/nsapplication/postevent(_:atstart:).md)
