鼠标离开NSWindow实例的事件

问题描述 投票:1回答:1

是否存在检测小鼠何时离开活跃的NSWindow边界的机制?

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

macos cocoa mouseevent nswindow appkit
1个回答
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];
}
© www.soinside.com 2019 - 2024. All rights reserved.