Contents

NSEvent

An object that contains information about an input action, such as a mouse click or a key press.

Declaration

class NSEvent

Overview

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

Getting the event type

Getting general event information

Getting modifier flags

Getting key event information

Getting mouse event information

Getting scroll wheel and flick events

Configuring swipe event behaviors

Getting gesture and touch information

Getting pressure information

Getting tablet proximity information

Getting tablet pointing information

Getting tracking area information

Getting custom event information

Requesting and stopping periodic events

Monitoring app events

Converting a mouse event’s position into a SpriteKit node’s coordinate space

Deprecated

Initializers

See Also

Mouse, Keyboard, and Touch Events