Setting the Current Cursor

An application may use several cursor instances—for example, one that looks like an arrow and one that looks like an I-beam. The instance that currently appears on the screen is called the “current cursor,” and is referenced by the currentCursor class method. You can set the current cursor in several ways:

The cursor rectangle is a region inside an NSView that triggers a change in the current cursor. To create a cursor rectangle, use the addCursorRect:cursor: method of NSView to associate a region of the view with the cursor, as shown in the example that follows. To make the association persistent, you can call addCursorRect:cursor: from within an override of resetCursorRects method of NSView, as described in Cocoa Event Handling Guide.

[aView addCursorRect:aRect cursor:aCursor];
[aCursor setOnMouseEntered:YES];

This assignment means that when the mouse enters aRect, aCursor receives a mouseEntered: event message, which the cursor uses to make itself the current cursor. However, before the cursor can acknowledge the mouseEntered: message, you must invoke the cursor’s setOnMouseEntered: method. Alternatively, you can set the cursor when the mouse leaves the cursor rectangle by invoking the setOnMouseExited: method instead of setOnMouseEntered:. A cursor that sets itself upon leaving the cursor rectangle receives a mouseExited: event message to instigate the change.

The Application Kit provides two ready-made cursors for commonly used cursor images. You can retrieve these cursors by using the arrowCursor and IBeamCursor class methods. There is no NSCursor instance for the wait cursor, because the system automatically displays it at the appropriate times.