繁体   English   中英

在未连接到窗口的NSView中使用NSTrackingArea?

[英]Using NSTrackingArea in an NSView that is unattached to a window?

基本上,我想要一个覆盖整个屏幕的“隐形”NSView。 我将添加一个NSTrackingArea,以便当我的光标在屏幕上移动时我得到全局鼠标事件。

-(void)setTrackingArea 
{
    view = [[NSView alloc] initWithFrame:[NSScreen currentScreenForPoint:[NSEvent mouseLocation]].frame];

    NSTrackingArea *area = [[NSTrackingArea alloc] initWithRect:[NSScreen currentScreenForPoint:[NSEvent mouseLocation]].frame options:NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways owner:view userInfo:nil];

    [view addTrackingArea:area];

    [area release];

    //[[window contentView] addSubview:view];

    //I don't want to add the view to a window, as all tutorials say.
}

- (void)mouseExited:(NSEvent *)theEvent 
{
    NSLog(@"Exit"); //Never firing
}

这可能吗? 在没有窗口的情况下使用NSView和NSTracking区域?

使用隐形视图绝对不是您想要做的事情。 看看进入addGlobalMonitorForEventsMatchingMask::对类方法NSEvent

例如,以下是为鼠标移动添加监视器的方法:

[NSEvent addGlobalMonitorForEventsMatchingMask:NSMouseMovedMask handler:^(NSEvent *mouseMovedEvent) {
    //do something with that event
}];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM