| Inherits from | |
| Conforms to | |
| Framework | /System/Library/Frameworks/AppKit.framework |
| Availability | Available in Mac OS X v10.0 and later.
|
| Companion guide | |
| Declared in | NSEvent.h |
An NSEvent object, or simply an event, contains information about an input action such as a mouse click or a key down. The Application Kit associates each such user action with a window, reporting the event to the application that created the window. The NSEvent object contains pertinent information about each event, such as where the cursor was located or which character was typed. As the application receives events, it temporarily places them in a buffer called the event queue. When the application is ready to process an event, it takes one from the queue.
Beginning with Mac OS X version 10.4, NSEvent objects can represent tablet-pointing and tablet-proximity events. A tablet-proximity event is generated when a pointing device enters or leaves proximity of its tablet; such event objects have a type of NSTypeProximity or a mouse subtype of NSTabletProximityEventSubtype. A tablet-pointing event is generated when a pointing device changes state, such as location, pressure, or tilt; such event objects have a type of NSTypePoint or a mouse subtype of NSTabletPointEventSubtype. The Application Kit reports all pure tablet events to responder objects through the NSResponder methods tabletPoint: and tabletProximity:. Mouse events can also contain tablet data (as event subtypes), so you can handle these events by overriding the NSResponder methods mouseDown:, mouseDragged:, and mouseUp:.
+ keyEventWithType:location:modifierFlags:timestamp:windowNumber:context:characters:charactersIgnoringModifiers:isARepeat:keyCode:
+ mouseEventWithType:location:modifierFlags:timestamp:windowNumber:context:eventNumber:clickCount:pressure:
+ enterExitEventWithType:location:modifierFlags:timestamp:windowNumber:context:eventNumber:trackingNumber:userData:
+ otherEventWithType:location:modifierFlags:timestamp:windowNumber:context:subtype:data1:data2:
+ eventWithEventRef:
– capabilityMask
– deviceID
– isEnteringProximity
– pointingDeviceID
– pointingDeviceSerialNumber
– pointingDeviceType
– systemTabletID
– tabletID
– uniqueID
– vendorID
– vendorPointingDeviceType
– absoluteX
– absoluteY
– absoluteZ
– buttonMask
– rotation
– tangentialPressure
– tilt
– vendorDefined
Returns a new NSEvent object describing a tracking-rectangle or cursor-update event.
+ (NSEvent *)enterExitEventWithType:(NSEventType)type location:(NSPoint)location modifierFlags:(NSUInteger)flags timestamp:(NSTimeInterval)time windowNumber:(NSInteger)windowNumber context:(NSGraphicsContext *)context eventNumber:(NSInteger)eventNumber trackingNumber:(NSInteger)trackingNumber userData:(void *)userData
One of the following event-type constants: NSMouseEntered, NSMouseExited, NSCursorUpdate. If the specified constant is not one of these, an NSInternalInconsistencyException is raised
The cursor location in the base coordinate system of the window specified by windowNum.
An integer bit field containing any of the modifier key masks described in “Constants,” combined using the C bitwise OR operator.
The time the event occurred in seconds since system startup.
An integer that identifies the window device associated with the event, which is associated with the NSWindow that will receive the event.
The display graphics context of the event.
An identifier for the new event. It’s normally taken from a counter for mouse events, which continually increases as the application runs.
A number that identifies the tracking rectangle. This identifier is the same as that returned by the NSView method addTrackingRect:owner:userData:assumeInside:.
Data arbitrarily associated with the tracking rectangle when it was set up using the NSView method addTrackingRect:owner:userData:assumeInside:.
The created NSEvent object or nil if the object could not be created.
NSEvent.hCreates an NSEvent object that is based on a Carbon type of event.
+ (NSEvent *)eventWithEventRef:(const void *)eventRef
The EventRef opaque type to be associated with the created NSEvent object.
An autoreleased NSEvent object corresponding to eventRef or nil if eventRef cannot be converted into an equivalent NSEvent object.
This method is valid for all events. The created NSEvent object retains the EventRef object and is released when the NSEvent object is freed.
NSEvent.h
Returns a new NSEvent object describing a key event.
+ (NSEvent *)keyEventWithType:(NSEventType)type location:(NSPoint)location modifierFlags:(NSUInteger)flags timestamp:(NSTimeInterval)time windowNumber:(NSInteger)windowNum context:(NSGraphicsContext *)context characters:(NSString *)characters charactersIgnoringModifiers:(NSString *)unmodCharacters isARepeat:(BOOL)repeatKey keyCode:(unsigned short)code
One of the following event-type constants: NSKeyDown, NSKeyUp, NSFlagsChanged. If anything else is specified, an NSInternalInconsistencyException is raised.
The cursor location in the base coordinate system of the window specified by windowNum.
An integer bit field containing any of the modifier key masks described in “Constants,” combined using the C bitwise OR operator.
The time the event occurred in seconds since system startup.
An integer that identifies the window device associated with the event, which is associated with the NSWindow that will receive the event.
The display graphics context of the event.
A string of characters associated with the key event. Though most key events contain only one character, it is possible for a single keypress to generate a series of characters.
The string of characters generated by the key event as if no modifier key had been pressed (except for Shift). This argument is useful for getting the “basic” key value in a hardware-independent manner.
YES if the key event is a repeat caused by the user holding the key down, NO if the key event is new.
A number that identifies the keyboard key associated with the key event. Its value is hardware-independent.
The created NSEvent instance or nil if the instance could not be created.
NSEvent.h
Returns a new NSEvent object describing a mouse-down, -up, -moved, or -dragged event.
+ (NSEvent *)mouseEventWithType:(NSEventType)type location:(NSPoint)location modifierFlags:(NSUInteger)flags timestamp:(NSTimeInterval)time windowNumber:(NSInteger)windowNum context:(NSGraphicsContext *)context eventNumber:(NSInteger)eventNumber clickCount:(NSInteger)clickNumber pressure:(float)pressure
One of the modifier key masks described in “Constants,” or an NSInternalInconsistencyException is raised.
The cursor location in the base coordinate system of the window specified by windowNum.
An integer bit field containing any of the modifier key masks described in “Constants,” combined using the C bitwise OR operator.
The time the event occurred in seconds since system startup.
An integer that identifies the window device associated with the event, which is associated with the NSWindow that will receive the event.
The display graphics context of the event.
An identifier for the new event. It’s normally taken from a counter for mouse events, which continually increases as the application runs.
The number of mouse clicks associated with the mouse event.
A value from 0.0 to 1.0 indicating the pressure applied to the input device on a mouse event, used for an appropriate device such as a graphics tablet. For devices that aren’t pressure-sensitive, the value should be either 0.0 or 1.0.
The created NSEvent instance or nil if the instance could not be created.
NSEvent.h
Reports the current mouse position in screen coordinates.
+ (NSPoint)mouseLocation
This method is similar to the NSWindow method mouseLocationOutsideOfEventStream. It returns the location regardless of the current event or pending events. The difference between these methods is that mouseLocationOutsideOfEventStream returns a point in the receiving window’s coordinates and mouseLocation returns the same information in screen coordinates.
NSEvent.h
Returns a new NSEvent object describing a custom event.
+ (NSEvent *)otherEventWithType:(NSEventType)type location:(NSPoint)location modifierFlags:(NSUInteger)flags timestamp:(NSTimeInterval)time windowNumber:(NSInteger)windowNum context:(NSGraphicsContext *)context subtype:(short)subtype data1:(NSInteger)data1 data2:(NSInteger)data2
One of the following event-type constants:
If type is anything else, an NSInternalInconsistencyException is raised. Your code should only create events of type NSApplicationDefined.
The cursor location in the base coordinate system of the window specified by windowNum.
An integer bit field containing any of the modifier key masks described in “Constants,” combined using the C bitwise OR operator.
The time the event occurred in seconds since system startup.
An integer that identifies the window device associated with the event, which is associated with the NSWindow that will receive the event.
The display graphics context of the event.
A numeric identifier that further differentiates custom events of types NSAppKitDefined, NSSystemDefined, and NSApplicationDefined. NSPeriodic events don’t use this attribute.
Additional data associated with the event. NSPeriodic events don’t use these attributes.
Additional data associated with the event. NSPeriodic events don’t use these attributes.
The created NSEvent object or nil if the object couldn't be created.
NSEvent.h
Begins generating periodic events for the current thread.
+ (void)startPeriodicEventsAfterDelay:(NSTimeInterval)delaySeconds withPeriod:(NSTimeInterval)periodSeconds
The number of seconds that NSEvent should wait before beginning to generate periodic events.
The period in seconds between the generated events.
Raises an NSInternalInconsistencyException if periodic events are already being generated for the current thread. This method is typically used in a modal loop while tracking mouse-dragged events.
NSEvent.h
Stops generating periodic events for the current thread and discards any periodic events remaining in the queue.
+ (void)stopPeriodicEvents
This message is ignored if periodic events aren’t currently being generated.
NSEvent.hReports the absolute x coordinate of a pointing device on its tablet at full tablet resolution.
- (NSInteger)absoluteX
For the coordinate to be valid, the receiver should represent an event generated by a tablet pointing device (otherwise 0 is returned). This method is valid only for mouse events with a subtype of NSTabletPointEventSubtype and for events of type NSTabletPoint. Use this value if you want to scale from tablet location to screen location yourself; otherwise use the class method mouseLocation or the instance method locationInWindow.
NSEvent.hReports the absolute y coordinate of a pointing device on its tablet at full tablet resolution.
- (NSInteger)absoluteY
For the coordinate to be valid, the receiver should represent an event generated by a tablet pointing device (otherwise 0 is returned). This method is valid only for mouse events with a subtype of NSTabletPointEventSubtype and for events of type NSTabletPoint. Use this value if you want to scale from tablet location to screen location yourself; otherwise use the class method mouseLocation or the instance method locationInWindow.
NSEvent.hReports the absolute z coordinate of pointing device on its tablet at full tablet resolution.
- (NSInteger)absoluteZ
For the coordinate to be valid, the receiver should represent an event generated by a tablet pointing device (otherwise 0 is returned). The z coordinate does not represent pressure. It registers the depth coordinate returned by some tablet devices with wheels; if the device is something other than these, 0 is returned. This method is valid only for mouse events with a subtype of NSTabletPointEventSubtype and for events of type NSTabletPoint.
NSEvent.hReturns a bit mask identifying the buttons pressed when the tablet event represented by the receiver was generated.
- (NSUInteger)buttonMask
Use one or more of the button-mask constants described in “Constants” to determine which buttons of the pointing device are pressed. This method is valid only for mouse events with a subtype of NSTabletPointEventSubtype and for events of type NSTabletPoint.
NSEvent.hReturns the button number for the mouse button that generated an NSOtherMouse... event.
- (NSInteger)buttonNumber
This method is intended for use with the NSOtherMouseDown, NSOtherMouseUp, and NSOtherMouseDragged events, but will return values for NSLeftMouse... and NSRightMouse... events also.
NSEvent.hReturns a mask whose set bits indicate the capabilities of the tablet device that generated the event represented by the receiver.
- (NSUInteger)capabilityMask
These bits are vendor-defined. This method is valid only for mouse events with a subtype of NSTabletProximityEventSubtype and for events of type NSTabletProximity.
NSEvent.hReturns the characters associated with the receiving key-up or key-down event.
- (NSString *)characters
These characters are derived from a keyboard mapping that associates various key combinations with Unicode characters. Raises an NSInternalInconsistencyException if sent to any other kind of event object.
This method returns an empty string for dead keys, such as Option-e. However, for a key combination such as Option-Shift-e this method returns the standard accent ("´").
– charactersIgnoringModifiers+ keyEventWithType:location:modifierFlags:timestamp:windowNumber:context:characters:charactersIgnoringModifiers:isARepeat:keyCode:NSEvent.hReturns the characters generated by the receiving key event as if no modifier key (except for Shift) applies.
- (NSString *)charactersIgnoringModifiers
Raises an NSInternalInconsistencyException if sent to a nonkey event.
This method returns the non-modifier key character pressed for dead keys, such as Option-e. For example, Option-e (no shift key) returns an “e" for this method, whereas the characters method returns an empty string.
This method is useful for determining “basic” key values in a hardware-independent manner, enabling such features as keyboard equivalents defined in terms of modifier keys plus character keys. For example, to determine if the user typed Alt-S, you don’t have to know whether Alt-S generates a German double ess, an integral sign, or a section symbol. You simply examine the string returned by this method along with the event’s modifier flags, checking for “s” and NSAlternateKeyMask.
– characters– modifierFlags+ keyEventWithType:location:modifierFlags:timestamp:windowNumber:context:characters:charactersIgnoringModifiers:isARepeat:keyCode:NSEvent.hReturns the number of mouse clicks associated with the receiver, which represents a mouse-down or mouse-up event.
- (NSInteger)clickCount
Raises an NSInternalInconsistencyException if sent to a nonmouse event.
Returns 0 for a mouse-up event if a time threshold has passed since the corresponding mouse-down event. This is because if this time threshold passes before the mouse button is released, it is no longer considered a mouse click, but a mouse-down event followed by a mouse-up event.
The return value of this method is meaningless for events other than mouse-down or mouse-up events.
NSEvent.hReturns the display graphics context of the receiver.
- (NSGraphicsContext *)context
NSEvent.hReturns additional data associated with the receiver.
- (NSInteger)data1
The value returned by this method is dependent on the event type, and is defined by the originator of the event. Raises an NSInternalInconsistencyException if sent to an event not of type NSAppKitDefined, NSSystemDefined, NSApplicationDefined, or NSPeriodic.
NSPeriodic events don’t use this attribute.
– data2– subtype+ otherEventWithType:location:modifierFlags:timestamp:windowNumber:context:subtype:data1:data2:NSEvent.hReturns additional data associated with the receiver.
- (NSInteger)data2
The value returned by this method is dependent on the event type, and is defined by the originator of the event. Raises an NSInternalInconsistencyException if sent to an event not of type NSAppKitDefined, NSSystemDefined, NSApplicationDefined, or NSPeriodic.
NSPeriodic events don’t use this attribute.
– data1– subtype+ otherEventWithType:location:modifierFlags:timestamp:windowNumber:context:subtype:data1:data2:NSEvent.hReturns the x-coordinate change for a scroll wheel, mouse-move, or mouse-drag event.
- (CGFloat)deltaX
NSEvent.hReturns the y-coordinate change for a scroll wheel, mouse-move, or mouse-drag event.
- (CGFloat)deltaY
The behavior of this method may seem counter-intuitive: as the mouse moves up the screen, the value is negative; and as it moves down the screen, the value is positive. The reason for this behavior is that NSEvent computes this delta value in device space, which is flipped, but both the screen and the window’s base coordinate system are not flipped.
NSEvent.hReturns the z-coordinate change for a scroll wheel, mouse-move, or mouse-drag event.
- (CGFloat)deltaZ
This value is typically 0.0.
NSEvent.hReturns a special identifier that is used to match tablet-pointer events with the tablet-proximity event represented by the receiver.
- (NSUInteger)deviceID
All tablet-pointer events generated in the period between the device entering and leaving tablet proximity have the same device ID. This message is valid only for mouse events with subtype NSTabletPointEventSubtype or NSTabletProximityEventSubtype, and for NSTabletPoint and NSTabletProximity events.
NSEvent.h
Returns the counter value of the latest mouse or tracking-rectangle event object; every system-generated mouse and tracking-rectangle event increments this counter.
- (NSInteger)eventNumber
Raises an NSInternalInconsistencyException if sent to any other type of event object.
+ enterExitEventWithType:location:modifierFlags:timestamp:windowNumber:context:eventNumber:trackingNumber:userData:+ mouseEventWithType:location:modifierFlags:timestamp:windowNumber:context:eventNumber:clickCount:pressure:NSEvent.hReturns the Carbon type associated with the receiver for representing an event.
- (const void *)eventRef
Returns an EventRef opaque type corresponding to the receiver. User-input events typically are created with an associated EventRef. An NSEvent object created through other means creates an EventRef in this method if that is necessary and possible. If there is no equivalent NSEvent for the receiver, this method returns NULL.
This method is valid for all types of events. The EventRef object is retained by the receiver, so it is valid as long as the NSEvent object is valid, and is released when the NSEvent object is freed. You can use RetainEvent to extend the lifetime of the EventRef object, with a corresponding ReleaseEvent when you are done with it.
NSEvent.hReturns YES if the receiving key event is a repeat caused by the user holding the key down, NO if the key event is new.
- (BOOL)isARepeat
Raises an NSInternalInconsistencyException if sent to an NSFlagsChanged event or other nonkey event.
NSEvent.hReturns YES to indicate that a pointing device is entering the proximity of its tablet and NO when it is leaving it.
- (BOOL)isEnteringProximity
This method is valid for mouse events with subtype NSTabletProximityEventSubtype and for NSTabletProximity events.
NSEvent.hReturns the virtual key code for the keyboard key associated with the receiving key event.
- (unsigned short)keyCode
The virtual key code. The returned value is hardware-independent. The value returned is the same as the value returned in the kEventParamKeyCode when using Carbon Events.
Raises an NSInternalInconsistencyException if sent to a non-key event.
NSEvent.hReturns the receiver’s location in the base coordinate system of the associated window.
- (NSPoint)locationInWindow
For nonmouse events the return value of this method is undefined.
With NSMouseMoved and possibly other events, the receiver can have a nil window (that is, window returns nil). In this case, locationInWindow returns the event location in screen coordinates.
In a method of a custom view that handles mouse events, you commonly use the locationInWindow method in conjunction with the NSView method convertPoint:fromView:to get the mouse location in the view’s coordinate system. For example:
NSPoint event_location = [theEvent locationInWindow]; |
NSPoint local_point = [self convertPoint:event_location fromView:nil]; |
NSEvent.hReturns an integer bit field indicating the modifier keys in effect for the receiver.
- (NSUInteger)modifierFlags
You can examine individual flag settings using the C bitwise AND operator with the predefined key masks described in “Constants.” The lower 16 bits of the modifier flags are reserved for device-dependent bits.
NSEvent.hReturns the index of the pointing device currently in proximity with the tablet.
- (NSUInteger)pointingDeviceID
This index is significant for multimode (or Dual Tracking) tablets that support multiple concurrent pointing devices; the index is incremented for each pointing device that comes into proximity. Otherwise, zero is always returned. The receiver of this message should be a mouse event object with subtype NSTabletProximityEventSubtype or an event of type NSTabletProximity.
NSEvent.hReturns the vendor-assigned serial number of a pointing device of a certain type.
- (NSUInteger)pointingDeviceSerialNumber
Devices of different types, such as a puck and a pen, may have the same serial number. The receiver of this message should be a mouse event object with subtype NSTabletProximityEventSubtype or an event of type NSTabletProximity.
NSEvent.hReturns a NSPointingDeviceType constant indicating the kind of pointing device associated with the receiver.
- (NSPointingDeviceType)pointingDeviceType
For example, the device could be a pen, eraser, or cursor pointing device. This method is valid for mouse events with subtype NSTabletProximityEventSubtype and for NSTabletProximity events. See “Constants” for descriptions of valid NSPointingDeviceType constants.
NSEvent.hReturns a value from 0.0 through 1.0 indicating the pressure applied to the input device (used for appropriate devices).
- (float)pressure
For devices that aren’t pressure-sensitive, the value is either 0.0 or 1.0. Raises an NSInternalInconsistencyException if sent to a nonmouse event.
For tablet pointing devices that are in proximity, the pressure value is 0.0 if they are not actually touching the tablet. As the device is pressed into the tablet, the value is increased.
+ mouseEventWithType:location:modifierFlags:timestamp:windowNumber:context:eventNumber:clickCount:pressure:– rotationNSEvent.hReturns the rotation in degrees of the tablet pointing device associated with the receiver.
- (float)rotation
Many devices do not support rotation, in which case the returned value is 0.0. This method is valid only for mouse events with subtype NSTabletPointEventSubtype and for NSTabletPoint events.
NSEvent.hReturns the subtype of the receiving event object.
- (short)subtype
Raises an NSInternalInconsistencyException if sent to an event not of type NSAppKitDefined, NSSystemDefined, NSApplicationDefined, or NSPeriodic.
NSPeriodic events don’t use this attribute.
This method is also valid for mouse events on Mac OS X v10.4 and later. See “Constants” for the predefined mouse and tablet subtypes.
– data1– data2+ otherEventWithType:location:modifierFlags:timestamp:windowNumber:context:subtype:data1:data2:NSEvent.hReturns the index of the tablet device connected to the system.
- (NSUInteger)systemTabletID
If multiple tablets are connected to the system, the system-tablet ID is incremented for each subsequent one. If there is only one tablet device, its system-tablet ID is zero. The receiver of this message should be a mouse event object with subtype NSTabletProximityEventSubtype or an event of type NSTabletProximity.
NSEvent.hReturns the USB model identifier of the tablet device associated with the receiver.
- (NSUInteger)tabletID
This method is valid for mouse events with subtype NSTabletProximityEventSubtype and for NSTabletProximity events.
NSEvent.hReports the tangential pressure on the device that generated the event represented by the receiver.
- (float)tangentialPressure
The value returned can range from -1.0 to 1.0. Tangential pressure is also known as barrel pressure. Only some pointing devices support tangential pressure. This method is valid for mouse events with subtype NSTabletPointEventSubtype and for NSTabletPoint events.
NSEvent.hReports the scaled tilt values of the pointing device that generated the event represented by the receiver.
- (NSPoint)tilt
The value returned can range from -1.0 to 1.0 for both axes. An x-coordinate value that is negative indicates a tilt to the left and a positive value indicates a tilt to the right; a y-coordinate value that is negative indicates a tilt to the top and a positive value indicates a tilt to the bottom. If the device is perfectly perpendicular to the table surface, the values are 0.0 for both axes. This method is valid for mouse events with subtype NSTabletPointEventSubtype and for NSTabletPoint events.
NSEvent.hReturns the time the receiver occurred in seconds since system startup.
- (NSTimeInterval)timestamp
NSEvent.hReturns the NSTrackingArea object that generated the event represented by the receiver.
- (NSTrackingArea *)trackingArea
Returns the NSTrackingArea object that generated the event represented by the receiver. If the receiver is not a mouse-tracking event (that is, an event of type NSMouseEntered, NSMouseExited, or NSCursorUpdate), this method raises an NSInternalInconsistencyException. This method returns nil if the event was generated by a tracking rectangle (pre-Mac OS X version 10.5) instead of a NSTrackingArea object.
If no NSTrackingArea object is associated with the event because the event corresponds to a tracking rectangle installed with the NSView method addTrackingRect:owner:userData:assumeInside:, this method returns nil. Note that the trackingNumber method returns either an NSTrackingArea object or the NSTrackingRectTag constant depending on how the event was generated.
NSEvent.hReturns the identifier of a mouse-tracking event.
- (NSInteger)trackingNumber
This method returns either an NSTrackingArea object or a NSTrackingRectTag constant depending on whether the event was generated from an NSTrackingArea object or a call to addTrackingRect:owner:userData:assumeInside:. Valid mouse-tracking methods are of types NSMouseEntered, NSMouseExited, and NSCursorUpdate. This method raises an NSInternalInconsistencyException if sent to any other type of event.
The NSTrackingArea class is new with Mac OS X version 10.5
+ enterExitEventWithType:location:modifierFlags:timestamp:windowNumber:context:eventNumber:trackingNumber:userData:– trackingAreaNSEvent.hReturns the type of the receiving event.
- (NSEventType)type
The type must be one of the following: