显示窗口后暂时禁用鼠标事件

时间:2019-05-07 08:53:03

标签: objective-c cocoa nswindow appkit

enter image description here

在Safari窗口中输入URL时,将出现子提示窗口。它会暂时隐藏并禁用鼠标输入。怎么做?

我有一个项目(下面的链接),该项目实现了建议位置+鼠标隐藏的位置。我不知道如何在键入时将鼠标置于建议窗口上方时禁用鼠标以停止选择建议(它总是选择其中一些条目)。

我知道隐藏的魔法应该与NSTrackingArea

有关
/* Properly creates a tracking area for an image view.
*/
- (id)trackingAreaForView:(NSView *)view {
    // make tracking data (to be stored in NSTrackingArea's userInfo) so we can later determine the imageView without hit testing
    NSDictionary *trackerData = [NSDictionary dictionaryWithObjectsAndKeys:view, kTrackerKey, nil];

    NSRect trackingRect = [[self.window contentView] convertRect:view.bounds fromView:view];
    NSTrackingAreaOptions trackingOptions = NSTrackingEnabledDuringMouseDrag | NSTrackingMouseEnteredAndExited | NSTrackingActiveInActiveApp;
    NSTrackingArea *trackingArea = [[NSTrackingArea alloc] initWithRect:trackingRect options:trackingOptions owner:self userInfo:trackerData];

    return trackingArea;
}

/* The mouse is now over one of our child image views. Update selection and send action.
*/
- (void)mouseEntered:(NSEvent*)event {
    HighlightingView *view = [(NSDictionary*)[event userData] objectForKey: kTrackerKey];
    [self userSetSelectedView:view];
}

项目: https://github.com/xhruso00/MapkitSearchFieldOSX

建议?苹果如何禁用鼠标输入以获取建议(地图...)

PS:我想继续强调建议。不想禁用它。

1 个答案:

答案 0 :(得分:0)

NSTrackingMouseMoved添加到NSTrackingMouseEnteredAndExited并记住哪个视图位于mouseEnter下:解决了该问题。

@interface SuggestionsWindowController() {
    HighlightingView *_mouseEnteredView; //ADDED
}



///* The mouse is now over one of our child image views. Update selection and send action.
//*/
- (void)mouseEntered:(NSEvent*)event {
    HighlightingView *view = [(NSDictionary*)[event userData] objectForKey: kTrackerKey];
    _mouseEnteredView = view; //ADDED
    //[self userSetSelectedView:view];
}

//ADDED
- (void)mouseMoved:(NSEvent*)event {
    //userData is not attached to event and it will crash if one accesses it
    //HighlightingView *view = [(NSDictionary*)[event userData] objectForKey: kTrackerKey];

    [self userSetSelectedView:_mouseEnteredView];
}

/* The mouse has left one of our child image views. Set the selection to no selection and send action
*/
- (void)mouseExited:(NSEvent*)event {
    [self userSetSelectedView:nil];
    _mouseEnteredView = nil; //ADDED
}