NSEvent
An object that contains information about an input action, such as a mouse click or a key press.
Declaration
class NSEventOverview
AppKit reports events that occur in a window to the app that created the window. Events include mouse clicks, key presses, and other types of input to the system. An NSEvent object contains pertinent information about each event, such as the event type and when the event occurred. The event type defines what other information is available in the event object. For example, a keyboard event contains information about the pressed keys.
Although you can create NSEvent objects directly, you typically don’t. The system generates them automatically in response to input from the mouse, keyboard, trackpad, or other peripherals such as connected tablets. It enqueues those events in its event queue, and dequeues them when it’s ready to process them. The system delivers events to the most relevant NSResponder object, which might be the first responder or the object where the event occurred. For example, the system delivers mouse-click events to the view that contains the event location.
To handle events, add support to your app’s NSResponder objects. You can also use gesture recognizers to handle some events for you and execute your app’s code at appropriate times. For more information, see the NSResponder reference.
You can also monitor the events your app receives and modify or cancel some events as needed. Install a local monitor using the addLocalMonitorForEvents(matching:handler:) method to detect specific types of events and take action when your app receives them. Install a global monitor using the addGlobalMonitorForEvents(matching:handler:) method to monitor events systemwide, although without the ability to modify them.
Topics
Creating an event object
keyEvent(with:location:modifierFlags:timestamp:windowNumber:context:characters:charactersIgnoringModifiers:isARepeat:keyCode:)mouseEvent(with:location:modifierFlags:timestamp:windowNumber:context:eventNumber:clickCount:pressure:)enterExitEvent(with:location:modifierFlags:timestamp:windowNumber:context:eventNumber:trackingNumber:userData:)otherEvent(with:location:modifierFlags:timestamp:windowNumber:context:subtype:data1:data2:)init(eventRef:)init(cgEvent:)
Getting the event type
Getting general event information
Getting modifier flags
Getting key event information
characterscharactersIgnoringModifierskeyCodecharacters(byApplyingModifiers:)keyRepeatDelaykeyRepeatIntervalspecialKeyFunction-Key Unicode ValuesNSEvent.SpecialKeyisARepeat
Getting mouse event information
Getting scroll wheel and flick events
deltaXdeltaYdeltaZhasPreciseScrollingDeltasscrollingDeltaXscrollingDeltaYmomentumPhaseisDirectionInvertedFromDevice
Configuring swipe event behaviors
isSwipeTrackingFromScrollEventsEnabledtrackSwipeEvent(options:dampenAmountThresholdMin:max:usingHandler:)NSEvent.SwipeTrackingOptions
Getting gesture and touch information
phaseNSEvent.Phasemagnificationtouches(matching:in:)allTouches()touches(for:)coalescedTouches(for:)isMouseCoalescingEnabledNSEvent.GestureAxis
Getting pressure information
Getting tablet proximity information
capabilityMaskdeviceIDisEnteringProximitypointingDeviceIDpointingDeviceSerialNumberpointingDeviceTypeNSEvent.PointingDeviceTypesystemTabletIDtabletIDuniqueIDvendorIDvendorPointingDeviceType
Getting tablet pointing information
Getting tracking area information
Getting custom event information
Requesting and stopping periodic events
Monitoring app events
addGlobalMonitorForEvents(matching:handler:)addLocalMonitorForEvents(matching:handler:)removeMonitor(_:)