Apple Wireless Keyboard requires the passcode that appears on your Mac. Enter the 8-digit passcode on the keyboard, then press Return. Earlier models of Apple Wireless Mouse or Apple Magic Trackpad will connect automatically or show a dialog. If you see a dialog, click Pair or press Return. According to reports in the support community, the magic mouse 2 doesn't always support 2-button operations reliably. This is one of the problems that I have with the magic mouse that I currently use. Pressing the right top corner of the magic mouse is frequently recognized as a left-click instead of the intended right-click. Apple Magic Mouse 2. Apple Magic Mouse 2 is one of the best mouse for MacBook Pro. It is a Professional and sleek mouse by Apple. This is the upgrade over the previous Magic mouse. The Design of the new Magic Mouse 2 is pretty much identical to the design of the previous Magic mouse. It has a white plastic silver on top of an aluminum case. When you connect a Bluetooth mouse or trackpad to your iPad, a circular pointer appears on the display. Move the mouse or swipe on the trackpad just as you would with a desktop or notebook computer. You can adjust how quickly the pointer moves, along with other trackpad and mouse settings.
Mouse events are one of the two most frequent kinds of events handled by an application (the other kind being, of course, key events). Mouse clicks—which involve a user pressing and then releasing a mouse button—generally indicate selection, but what the selection means is left up to the object responding to the event. For example, a mouse click could tell the responding object to alter its appearance and then send an action message. Mouse drags generally indicate that the receiving view should move itself or a drawn object within its bounds. The following sections describe how you might handle mouse-down, mouse-up, and mouse-drag events.
Note: This chapter discusses those mouse events that an
NSWindow
object delivers to its views through the event-dispatch mechanism: mouse-down, mouse-up, mouse-dragged, and (to a lesser degree) mouse-moved events. It does not describe how to handle mouse-entered and mouse-exited events, which (possibly along with mouse-moved events) are used in mouse tracking. See Using Tracking-Area Objects for information on this subject.Overview of Mouse Events
Before going into the “how to” of mouse-event handling, let’s review some central facts about mouse events discussed in Event Architecture, Event Objects and Types, and Event Handling Basics:
- Mouse events are dispatched by an
NSWindow
object to theNSView
object over which the event occurred. - Subsequent key events are dispatched to that view object if it accepts first responder status.
- If the user clicks a view that isn’t in the key window, by default the window is brought forward and made key, but the mouse event is not dispatched.
- Mouse events are of various event types related to the mouse button pressed (left, right, other) and the nature of the action on the mouse button. Each event type is, in turn, related to an
NSResponder
method, as Table 4-1 shows for left-button mouse events.Table 4-1 Type constants and methods related to left-button mouse events ActionEvent type (left mouse button)Mouse-event method invoked (left mouse button)Press down the buttonMove the mouse while pressing the buttonRelease the buttonMove the mouse without pressing any buttonNote: Because mouse-moved events occur so frequently that they can quickly flood the event-dispatch machinery, anNSWindow
object by default does not receive them from the globalNSApplication
object. However, you can specifically request these events by sending theNSWindow
object ansetAcceptsMouseMovedEvents:
message with an argument ofYES
.Right-mouse events are defined by the Application Kit to open contextual menus, but you can override this behavior if necessary (in, for example,rightMouseDown:
). The Application Kit does not define any default behavior for third (“other”) mouse buttons. - The general sequence of mouse events is: mouse-down, mouse-dragged (multiple), mouse-up.If dispatch of them is turned on, mouse-moved events may only occur between a mouse-up and the next mouse-down. They do not occur between a mouse-down and the subsequent mouse-up.
- Mouse events (as
NSEvent
objects) can have subtypes. Currently, these subtypes are restricted to tablet events (particularly tablet-pointer events) but more subtypes could be added in the future. (See Tablet Events for more information.)
Handling Mouse Clicks
One of the earliest things to consider in handling mouse-down events is whether the receiving
NSView
object should become the first responder, which means that it will be the first candidate for subsequent key events and action messages. Views that handle graphic elements that the user can select—drawing shapes or text, for example—should typically accept first responder status on a mouse-down event by overriding the acceptsFirstResponder
method to return YES
, as discussed in Preparing a Custom View for Receiving Events. By default, a mouse-down event in a window that isn’t the key window simply brings the window forward and makes it key; the event isn’t sent to the
NSView
object over which the mouse click occurs. The NSView
can claim an initial mouse-down event, however, by overriding acceptsFirstMouse:
to return YES
. The argument of this method is the mouse-down event that occurred in the non-key window, which the view object can examine to determine whether it wants to receive the mouse event and potentially become first responder. You want the default behavior of this method in, for example, a control that affects the selected object in a window. However, in certain cases it’s appropriate to override this behavior, such as for controls that should receive mouseDown:
messages even when the window is inactive. Examples of controls that support this click-through behavior are the title-bar buttons of a window. Apple Single Button Mouse Pc
In your implementation of an
NSResponder
mouse-event method, often the first thing you might do is examine the passed-in NSEvent
object to decide if this is an event you want to handle. If it is an event that you handle, then you may need to extract information from the NSEvent
object to help you handle it. Specifically, you can get the following information from the NSEvent
object:- Get the location of the mouse event in base coordinates (
locationInWindow
) and then convert this location to the receiving view’s coordinate system; see Getting the Location of an Event for details. - See if any modifier keys were pressed when the mouse button was clicked (
modifierFlags
); this procedure is described in Testing for Event Type and Modifier Flags. An application may define modifier keys to change the significance of a mouse event. - Find out how many mouse clicks occurred in quick succession (
clickCount
); multiple mouse clicks are conceptually treated as a single mouse-down event within a narrow time threshold (although they arrive in a series ofmouseDown:
messages). As with modifier keys, a double- or triple-click can change the significance of a mouse event for an application. (See Listing 4-3 for an example.) - If the interval between mouse clicks is important, you can send
timestamp
to theNSEvent
object and record the moment each event occurred. - If the change in position of the mouse between subsequent events is important, you can find out the delta values for the x-coordinate and y-coordinate using, respectively,
deltaX
anddeltaY
.
Many view objects in the Application Kit (such as controls and menu items) change their appearance in response to mouse-down events, sometimes only until the subsequent mouse-up event. Doing this provides visual confirmation to the user that their action is effective or that the clicked object is now selected. Listing 4-1 shows a simple example of this.
Apple Single Button Mouse Setup
Listing 4-1 Simple handling of mouse click—changing view’s appearance
But many view objects, particularly controls and cells, do more than simply change their appearance in response to mouse clicks. One common paradigm is for the clicked view to send an action message to a target object (where both action and target are settable properties of the view). As shown in Listing 4-2, the view typically sends the message on
mouseUp:
rather than mouseDown:
, thus giving users an opportunity to change their minds mid-click. Listing 4-2 Simple handling of mouse click—sending an action message
Listing 4-3 gives a more complex, real-world example. (It’s from the example project for the Sketch application.) This implementation of
mouseDown:
determines if users double-clicked a graphical object and, if they did, enables the editing of that object. Otherwise, if a palette object is selected, it creates an instance of that object at the location of the mouse click. Listing 4-3 Handling a mouse-down event—Sketch application
The classes of the Application Kit that implement controls manage this target-action behavior for you.
Handling Mouse Dragging Operations
Mouse-down and mouse-up events, which are the events associated with mouse clicks, are not the only types of mouse events dispatched in an application. Views can also receive mouse-dragged events when a user moves a mouse while pressing down a mouse button. A view typically interprets mouse-dragged events as commands to move itself by altering its frame location or to move a region within its bounds by redrawing it. However, other interpretations of mouse-dragged events are possible; for example, a view could respond to mouse-dragged events by magnifying the region that the mouse pointer is dragged over.
With the Application Kit you can take one of two general approaches when handling mouse-dragged events. The first approach is overriding the three
NSResponder
methods mouseDown:
, mouseDragged:
, and mouseUp:
(for left mouse-button operations). For each dragging sequence, the Application Kit sends a mouseDown:
message to a responder object, then sends one or more mouseDragged:
messages, and ends the sequence with a mouseUp:
message. The Three-Method Approach describes this approach.The other approach treats the mouse events in a dragging sequence as a single event, from mouse down, through dragging, to mouse up. To do this, the responder object must usually short-circuit the application’s normal event loop by entering a event-tracking loop to filter and process only mouse events of interest. For example, an
NSButton
object highlights itself upon a mouse-down event, then follows the mouse location during dragging, highlighting when the mouse is inside and unhighlighting when the mouse is outside. If the mouse is inside on the mouse-up event, the button object sends its action message. This approach is described in The Mouse-Tracking Loop Approach. Both of these approaches have their advantages and drawbacks. Establishing a mouse-tracking loop gives you greater control over the way other events interact with your application during a dragging operation. However, the application’s main thread is unable to process any other requests during an event-tracking loop and timers might not fire as expected. The mouse-tracking approach is more efficient because it typically requires less code and allows all dragging variables to be local. However, the class implementing it becomes more difficult to extend without the subclass reimplementing all the dragging code.
Implementing the individual
mouseDown:
, mouseDragged:
, and mouseUp:
methods is often a better design choice when writing an event-driven application. Each of the methods have a clearly defined scope, which often leads to clearer code. This approach also makes it much easier for subclasses to override behavior for handling mouse-down, mouse-dragged, and mouse-up events. However, this technique can require more code and instance variables.The Three-Method Approach
To handle a mouse-dragging operation, you can override the three
NSResponder
methods that mark the discrete stages of a mouse-dragging operation: mouseDown:
, mouseDragged:
, and mouseUp:
(or, for right-mouse dragging, rightMouseDown:
, rightMouseDragged:
, and rightMouseUp:
). A mouse-dragging sequence consists of one mouseDown:
message, followed by (typically) multiple mouseDragged:
messages, and a concluding mouseUp:
message. The subclass implementing these methods often has to declare instance variables to hold the changing values or states of various things between successive events. These things could be geometric entities such as rectangles or points (corresponding to view frames or regions within views) or they could be simple Boolean values indicating, for example, that an object is selected. In the
mouseDown:
method, the view generally initializes any dragging-related instance variables; in the mouseDragged:
method it might update those instance variables or check them prior to performing an action; and in the mouseUp:
method, it often resets those instance variables to their initial values.Because mouse-dragging operations often redraw an object in the incrementally changing locations where users drag that object, implementations of the three mouse-dragging methods usually need to find the location of each mouse event in the view’s coordinate system. As explained in Getting the Location of an Event, this requires the view to send
locationInWindow
to the passed-in NSEvent
object and then use the convertPoint:fromView:
method to convert the resulting location to the local coordinate system. When changes in location or geometry require the view to redraw itself or a portion of itself, the view marks the areas needing display using the setNeedsDisplay:
or setNeedsDisplayInRect:
method and the view is later asked to redraw itself (in drawRect:
) through the auto-display mechanism (assuming that mechanism is not turned off). Listing 4-4 illustrates the use of dragging-related instance variables, getting the mouse location in local coordinates (and testing whether that location is in a specific region), and marking that region of the receiving view for redisplay.
Listing 4-4 Handling a mouse-dragging operation—three-method approach
Single Button Mouse Pc
The Mouse-Tracking Loop Approach
The mouse-tracking technique for handling mouse-dragging operations is applied in a single method, usually (but not necessarily) in
mouseDown:
. The implementing responder object first declares and possibly initializes one or more local variables to use within the loop. Often one of these variables holds a value, often Boolean, that is used to control the loop. When some condition is met, typically the receipt of a mouse-up event, the variable value is changed; when this variable is tested next time through the loop, control exits the loop.The central method of a mouse-tracking loop is the
NSApplication
method nextEventMatchingMask:untilDate:inMode:dequeue:
and the NSWindow
method of the same name. These methods fetch events from the event queue that are of the types specified by one or more type-mask constants; for mouse-dragging, these constants are typically NSLeftMouseDraggedMask
and NSLeftMouseUpMask
. Events of other types are left in the queue. (The run-loop mode parameter of both nextEventMatchingMask:untilDate:inMode:dequeue:
methods should be NSEventTrackingRunLoopMode
.)After receiving a mouse-down event, fetch subsequent mouse events within the loop using
nextEventMatchingMask:untilDate:inMode:dequeue:
. Process NSLeftMouseDragged
events as you would process them in the mouseDragged:
method (described in The Three-Method Approach); similarly, handle NSLeftMouseUp
events as you would handle them in mouseUp:
. Usually mouse-up events indicate that execution control should break out of the loop.The
mouseDown:
method template in Listing 4-5 shows one possible kind of modal event loop.Listing 4-5 Handling mouse-dragging in a mouse-tracking loop—simple example
This loop converts the mouse location and checks whether it’s inside the receiver. It highlights itself using the fictional
highlight:
method and, on receiving a mouse-up event, it invokes doSomethingSignificant
to perform an important action. Instead of merely highlighting, a custom NSView
object might move a selected object, draw a graphic image according to the mouse’s location, and so on.Listing 4-6 is a slightly more complicated example that includes the use of an autorelease pool and testing for a modifier key.
Listing 4-6 Handling mouse-dragging in a mouse-tracking loop—complex example
A mouse-tracking loop is driven only as long as the user actually moves the mouse. It won’t work, for example, to cause continual scrolling if the user presses the mouse button but never moves the mouse itself. For this, your loop should start a periodic event stream using the
NSEvent
class method startPeriodicEventsAfterDelay:withPeriod:
, and add NSPeriodicMask
to the mask bit-field passed to nextEventMatchingMask:
. In the switch
statement the implementing view object can then check for events of type NSPeriodic
and take whatever action it needs to—scrolling a document view or moving a step in an animation, for example. If you need to check the mouse location during a periodic event, you can use the NSWindow
method mouseLocationOutsideOfEventStream
.Filtering Out Key Events During Mouse-Tracking Operations
A potential problem with mouse-tracking code is the user pressing a key combination that is a command, such as Command-z (undo), while a tracking operation is underway. Because your mouse-tracking code (either in the three-method approach or a mouse-tracking loop) isn’t looking for that key event, the code might not know how to handle that key command or could handle it wrongly, with unwelcome consequences.
The problem here with a mouse-tracking loop might not be readily apparent because, after all, the
nextEventMatchingMask:untilDate:inMode:dequeue:
loop ensures only mouse-tracking events are delivered. Why would key events be a problem? Consider the code in Listing 4-7. While this mouse-tracking loop is active, let’s say the user issues some key-equivalent commands.Listing 4-7 Typical mouse-tracking loop
What happens after the mouse-tracking loop concludes? After the user lets go of the mouse button, the application handles all of the pending commands corresponding to the mnemonics the user pressed during the loop. The effects of this delayed handling are probably undesirable. You can guard against this by filtering the event stream for key events and then explicitly ignoring them. Listing 4-8 shows how you can modify the code above to accomplish this (new code in italics).
Listing 4-8 Typical mouse-tracking loop—with key events negated
For dragging operations handled with the three-method approach, the situation with simultaneous mouse and key events is a bit different. In this case, the AppKit processes keyboard events as it normally does during tracking. If the user presses a command mnemonic, even while a tracking operation is going on, the application object dispatches the corresponding messages to its targets. So if, for example, in a drawing application the user drags a blue circle they just created and then (perhaps accidentally) presses Command-x (cut) while the mouse button is still down, the code handling the cut operation is going to run, deleting the object the user was dragging.
The solution to this problem involves a few steps:
- Declare a Boolean instance variable that reflects when a dragging operation is underway.
- Set this variable to
YES
inmouseDown:
and reset it toNO
inmouseUp:
. - Override
performKeyEquivalent:
to check the value of the instance variable and, if a dragging operation is occurring, to discard the key event. Vray for sketchup 2017 free.
Listing 4-9 shows the implementation code for this (
isBeingManipulated
is the Boolean instance variable). Listing 4-9 Discarding key events during a dragging operation with the three-method approach
This solution to the problem is simplified and is intended for general illustration. In a real application you might want to check the event type, conditionally set the
isBeingManipulated
variable, and selectively handle key equivalents. Copyright © 2016 Apple Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2016-09-13
Apple's product design is legendary in the industry for innovation, inspiration and imagination, but it's often the more glamorous products such as the MacBooks, iPhones and iPods that get all the attention.
With this in mind, we thought we'd take a look at Apple's input device — the humble mouse — to see how the design has changed from way back in the early 80s right up to today.
The evolution of the Apple mouse makes for an interesting design journey that includes the loss of its tail, moving from a single button to touch, and of course going from beige to multi-colored and back to a monotone.
Click through the gallery below to journey through the mouse's history and let us know any memories you have (fond or not!) of any of the devices in the comments.
More Apple Resources from Mashable:
- 10 Geekiest Apple-Flavored Cakes [PICS] - 10 Best Apple iPad Stands - 10 Awesome Apple-Inspired iPad Wallpapers [PICS] - 10 Vintage Apple Ads That Time Forgot [VIDEOS] - 10 Fascinating Facts You Didn’t Know About Apple
Image courtesy of The Mothership