鼠标离开NSWindow实例的事件

时间:2019-02-17 21:38:57

标签: macos cocoa mouseevent nswindow appkit

是否有一种机制可以检测鼠标何时离开有效的NSWindow边界?

我已经尝试覆盖mouseMoved:方法,但是当鼠标位于NSWindow边界之外时不会调用该方法。

1 个答案:

答案 0 :(得分:3)

最好是使用NSTrackingArea

- (void)configureTrackingArea
{
    NSRect trackingRect = self.window.contentView.frame
    NSTrackingAreaOptions trackingOptions = NSTrackingMouseEnteredAndExited | NSTrackingActiveInKeyWindow;
    NSTrackingArea *trackingArea = [[NSTrackingArea alloc] initWithRect:trackingRect options:trackingOptions owner:self userInfo:nil];

    NSView *contentView = [[self window] contentView];
    [contentView addTrackingArea:trackingArea];
}


- (void)mouseEntered:(NSEvent *)event
{
    [[self window] addChildWindow:[self previewWindow] ordered:NSWindowAbove];
}

- (void)mouseExited:(NSEvent *)event
{
    [self hidePreviewWindow];
}