On occasion you might need to discover the type of an event. However, you do not need to do this within an event-handling method of NSResponder because the type of the event is apparent from the method name: rightMouseDragged:, keyDown:, tabletProximity:, and so on. But from elsewhere in an application you can always obtain the currently handled event by sending currentEvent to NSApp. To find out what type of event this is, send type to the NSEvent object and then compare the returned value with one of the NSEventType constants. Listing 3-5 gives an example of this.
Listing 3-5 Testing for event type
NSEvent *currentEvent = [NSApp currentEvent]; |
NSPoint mousePoint = [controlView convertPoint: [currentEvent locationInWindow] fromView:nil]; |
switch ([currentEvent type]) { |
case NSLeftMouseDown: |
case NSLeftMouseDragged: |
[self doSomethingWithEvent:currentEvent]; |
break; |
default: |
// If we find anything other than a mouse down or dragged we are done. |
return YES; |
} |
A common test performed in event-handling methods is finding out whether specific modifier keys were pressed at the same moment as a keypress, mouse click, or similar user action. The modifier key usually imparts special significance to the event. The code example in Listing 3-6 shows an implementation of mouseDown: that determines whether the Command key was pressed while the mouse was clicked. If it was, it rotates the receiver (a view) 90 degrees. The identification of modifier keys requires the event handler to send modifierFlags to the passed-in event object and then perform a bitwise-AND operation on the returned value using one or more of the modifier mask constants declared in NSEvent.h.
Listing 3-6 Testing for modifier keys pressed—event method
- (void)mouseDown:(NSEvent *)theEvent { |
// if Command-click rotate 90 degrees |
if ([theEvent modifierFlags] & NSCommandKeyMask) { |
[self setFrameRotation:[self frameRotation]+90.0]; |
[self setNeedsDisplay:YES]; |
} |
} |
You can test an event object to find out if any from a set of modifier keys was pressed by performing a bitwise-OR with the modifier-mask constants, as in Listing 3-7. (Note also that this example shows the use of the currentEvent method in a keyboard action method.)
Listing 3-7 Testing for modifier keys pressed—action method
- (void)moveLeft:(id)sender { |
// Use left arrow to decrement the time. If a shift key is down, use a big step size. |
BOOL shiftKeyDown = ([[NSApp currentEvent] modifierFlags] & |
(NSShiftKeyMask | NSAlphaShiftKeyMask)) !=0; |
[self incrementHour:0 andMinute:-(shiftKeyDown ? 15 : 1)]; |
} |
Further, you can look for certain modifier-key combinations by linking the individual modifier-key tests together with the logical AND operator (&&). For example, if the example method in Listing 3-6 were to look for Command-Shift-click rather than Command-click, the complete test would be the following:
if ( ( [theEvent modifierFlags] & NSCommandKeyMask ) && |
( [theEvent modifierFlags] & NSShiftKeyMask ) ) |
In addition to testing an NSEvent object for individual event types, you can also restrict the events fetched from the event queue to specified types. You can perform this filtering of event types in the nextEventMatchingMask:untilDate:inMode:dequeue: method (NSApplication and NSWindow) or the nextEventMatchingMask: method of NSWindow. The second parameter of all these methods takes one or more of the event-type mask constants declared in NSEvent.h—for example NSLeftMouseDraggedMask, NSFlagsChangedMask, and NSTabletProximityMask. You can either specify these constants individually or perform a bitwise-OR on them.
Because the nextEventMatchingMask:untilDate:inMode:dequeue: method is used almost exclusively in closed loops for processing a related series of mouse events, the use of it is described in “Handling Mouse Events.”
Last updated: 2007-03-16