Set it "permanently" via
% defaults write com.apple.TextEdit NSShowAllViews YES(or whatever your application identifier is), or use the one-shot version:
/Developer/Applications/Xcode.app/Contents/MacOS/Xcode -NSShowAllViews YES
In your mouseDown:
if ([event modifierFlags] & NSShiftKeyMask) {
constrain = YES;
}
If you need to check them outside of your mouseDown, and if you're on 10.6 or beyond, you can use +[NSEvent modifierFlags];
If you're stuck in 10.5 or older, you need to do something like this: dip into carbon:
(Thanks to Mike Ash for pointing out the new 10.6 API)
NSPoint point = [self convertPoint: [event locationInWindow] fromView: nil];
- (void) keyDown: (NSEvent *) event
{
NSString *characters;
characters = [event characters];
unichar character;
character = [characters characterAtIndex: 0];
if (character == NSRightArrowFunctionKey) {
[self moveSelectedBlockBy: 1];
} else if (character == NSLeftArrowFunctionKey) {
[self moveSelectedBlockBy: -1];
} ... and look at whatever other keys you want
}
NSView's viewWithTag: to find it.
[self lockFocus];
NSBitmapImageRep *bits;
bits = [[NSBitmapImageRep alloc]
initWithFocusedViewRect: [self bounds]];
[self unlockFocus];
Then you can add it to an NSImage or save it to a file, or whatever.
If you want to retain the vectoritude of the drawing, you can use [self dataWithPDFInsideRect: [self bounds]], and then make an NSPDFImageRep. Avoid the NSEPSImageRep since it makes a trip through NSPDFImageRep along with a slow PS to PDF conversion. (Thanks to Peter Hosey for the PDF hint)
NSEventTrackingRunLoopMode, such as you wanting to use an NSNotificationQueue to coalesce updates for your own mouse tracking code. I've been sticking a call to this in my mouseDown: handler for the cases when I want the secondary run loop
- (void) runEventTrackingRunLoop
{
NSEventType eventType = NSLeftMouseDown;
unsigned int eventMask = NSLeftMouseDownMask | NSLeftMouseUpMask
| NSLeftMouseDraggedMask | NSMouseMovedMask;
while (eventType != NSLeftMouseUp) {
NSEvent *event;
event = [NSApp nextEventMatchingMask: eventMask
untilDate: [NSDate distantFuture]
inMode: NSEventTrackingRunLoopMode
dequeue: YES];
eventType = [event type];
if (eventType == NSLeftMouseDragged) {
[self mouseDragged: event];
}
}
} // runEventTrackingRunLoop
(and thanks to Rainer Brockerhof for pointing out a no-op line of code from the original version of this)
@interface Blah : NSView
{
NSPoint grabOrigin;
NSPoint scrollOrigin;
}
@end // Blah
...
@implementation Blah
...
- (void) mouseDown: (NSEvent *) event
{
// deal in window coordinates. there is a scrolling problem
// if using view coordinates because view coordinates
// can get transformed
grabOrigin = [event locationInWindow];
NSClipView *contentView;
contentView = (NSClipView*)[layerView superview];
scrollOrigin = [contentView bounds].origin;
} // mouseDown
- (void) mouseDragged: (NSEvent *) event
{
NSPoint mousePoint;
mousePoint = [event locationInWindow];
float deltaX, deltaY;
deltaX = grabOrigin.x - mousePoint.x;
deltaY = mousePoint.y - grabOrigin.y;
NSPoint newOrigin;
newOrigin = NSMakePoint (scrollOrigin.x + deltaX,
scrollOrigin.y + deltaY);
[layerView scrollPoint: newOrigin];
} // mouseDragged
...
@end // Blah
You can be fancy and check for the option key by look at [event modifierFlags] and looking for NSAlternateKeyMask, and also use the NSCursor open/closedHandCursor.- (void) keyDown: (NSEvent *) event
{
NSString *chars = [event characters];
unichar character = [chars characterAtIndex: 0];
if (character == 27) {
NSLog (@"ESCAPE!");
}
} // keyDownNSDeleteCharacter in the event's character string:
- (void) keyDown: (NSEvent *) event
{
NSString *chars = [event characters];
unichar character = [chars characterAtIndex: 0];
if (character == NSDeleteCharacter) {
NSLog (@"Delete!");
}
} // keyDownflagsChanged: (NSEvent *) event and look at the event there. (flagsChanged: comes from NSResponder, so any responder in the responder chain can react to it)-[NSView setCursor:] call (drat!) You can get something similar by adding this to your NSView subclass:
- (void) resetCursorRects
{
[super resetCursorRects];
[self addCursorRect: [self bounds]
cursor: [NSCursor crosshairCursor]];
} // resetCursorRects
- (BOOL) _hasEditableCell
{
return (YES);
} // _hasEditableCell
This workaround is necessary at least for Mac OS X 10.1.*.
override var flipped: Bool {
return true
}