如何用Objective-C识别鼠标下的窗口?

时间:2012-09-22 01:40:52

标签: objective-c

我有以下代码:

id eventHandler = [NSEvent addGlobalMonitorForEventsMatchingMask:NSMouseMovedMask handler:^(NSEvent * event) {
    CGWindowID windowID = (CGWindowID)[event windowNumber];
    CFArrayRef a = CFArrayCreate(NULL, (void *)&windowID, 1, NULL);
    NSArray *windowInfos = (__bridge NSArray *)CGWindowListCreateDescriptionFromArray(a);
    CFRelease(a);
    if ([windowInfos count] > 0) {
        NSDictionary *windowInfo = [windowInfos objectAtIndex:0];
        NSLog(@"Name:  %@", [windowInfo objectForKey:(NSString *)kCGWindowName]);
        NSLog(@"Owner: %@", [windowInfo objectForKey:(NSString *)kCGWindowOwnerName]);
        //etc.
    }
}];

在识别光标下的窗口方面做得非常好。但是,为了可靠地识别它们,您必须单击窗口。有些窗口会触发更改,例如终端和xcode,但是悬停在桌面上并不会触发Finder在光标下。但是,如果您单击桌面,则会正确识别它。任何想法我可能会失踪?

0 个答案:

没有答案