マウスが動いているときのイベントを受け取る
(旧) Cocoaの日々: マウス移動のトラッキング
(旧) Cocoaの日々: NSTrackingArea
http://developer.apple.com/documentation/Cocoa/Reference/NSTrackingArea_class/Reference/Reference.html
http://developer.apple.com/documentation/Cocoa/Conceptual/EventOverview/MouseTrackingEvents/chapter_950_section_2.html#//apple_ref/doc/uid/10000060i-CH11-DontLinkElementID_26
デフォルトではドラッグしているときのイベントは受け取れるが、
フリーでカーソルを動かしたときのイベントは受け取れない。
こんな風にしてみた。
FirstResponderに。
#これでいいのか?
- (BOOL)acceptsFirstResponder { return YES; } - (BOOL)becomeFirstResponder { return YES; } - (BOOL)resignFirstResponder { return YES; }
initWithFrameに書く。あと、ウィンドウのリサイズを有効にしている場合には、リサイズのメソッドにも書く。
trackingRectはNSTrackingRectTag型のインスタンス変数
-(id) initWithFrame: (NSRect) frameRect { trackingRect = [self addTrackingRect:[self bounds] owner:self userData:NULL assumeInside:NO];
mouseEnteredでイベント受け取ることにして、mouseExitedでイベント受け取らないことにする。
- (void)mouseEntered:(NSEvent *)theEvent { [[self window] setAcceptsMouseMovedEvents:YES]; } - (void)mouseMoved:(NSEvent *)theEvent { NSPoint location = [self convertPoint:[theEvent locationInWindow] fromView:nil]; NSLog(@"x = %f y = %f " ,location.x, location.y); } - (void)mouseExited:(NSEvent *)theEvent { [[self window] setAcceptsMouseMovedEvents:NO]; }