简体   繁体   English

OSX-如何在所有空间(包括全屏)中识别光标下方的窗口

[英]OSX - How to identify window under cursor in all spaces including fullscreen

I'm using Window Services' CGWindowListCreate and CGWindowListCreateDescriptionFromArray to get window information. 我正在使用Window Services的CGWindowListCreateCGWindowListCreateDescriptionFromArray来获取窗口信息。 When getting kCGWindowBounds in a regular Space everything works fine (I'm drawing borders around the frontmost window on the 0th level). 当在常规空间中获取kCGWindowBounds ,一切正常(我在第0层的最前面的窗口周围绘制边框)。 However, when I use the same method while on a fullscreen application's Space, I get nonsense bounds: (0, 855, 480, 1). 但是,当我在全屏应用程序的Space上使用相同的方法时,我会胡说八道:(0、855、480、1)。

I wouldn't care much about this if there was an easy way to tell if I'm currently at a fullscreen app's Space, because then I'd just draw a border around the screen (well... it would depend if the menu bar is showing...). 我不会在意这一点,如果有一个简单的方法来告诉我,如果在全屏应用程序的空间目前是,因为那时我刚刚绘制屏幕周围的边框(嗯...这取决于如果菜单栏正在显示...)。

Is this a bug, or is there a reason for this behavior? 这是一个错误,还是有这种现象的原因?

EDIT: Figured out my problem. 编辑:弄清楚了我的问题。 It's a bigger issue than I would have liked. 这是一个比我想要的更大的问题。 The thing is the API goes through ALL NSWindows, even the ones that aren't, well, normal windows. 关键是API会通过所有NSWindows,甚至不是正常的Windows。 Chrome's loading bar on the bottom is a window by itself, for example, and Mail also has some window on the top of the app. 例如,Chrome底部的加载栏本身就是一个窗口,而Mail的顶部也有一些窗口。 This is a problem because I have no way to differentiate the window that looks to be frontmost. 这是一个问题,因为我无法区分看起来最靠前的窗口。

For my app, I would like to capture a specific window to intercept mouse events in it. 对于我的应用程序,我想捕获一个特定的窗口以拦截其中的鼠标事件。 I would have liked to be able to have the user press a hotkey and then click on the desired window to select, but there is no API to get the window under the cursor. 我希望能够让用户按下热键,然后单击所需的窗口进行选择,但是没有API可以将窗口移到光标下面。 I have no clue how to proceed. 我不知道如何进行。

Edit 2: To better help people find a useful answer, changed title from: "Quartz Window Services returning wrong window bounds for fullscreen apps" 编辑2:为了更好地帮助人们找到有用的答案,将标题更改为:“ Quartz Window Services为全屏应用程序返回了错误的窗口边界”

Have you got these methods defined for the window delegate? 您是否已为窗口委托定义了这些方法?

- (NSSize)window:(NSWindow *)window willUseFullScreenContentSize:(NSSize)proposedSize
{
    NSRect mainDisplayRect = [[NSScreen mainScreen] frame];
    CGSize cgScreenSize = CGSizeMake(mainDisplayRect.size.width, mainDisplayRect.size.height);

    return cgScreenSize;
}

- (void)windowWillEnterFullScreen:(NSNotification *)notification
{

}

- (void)windowDidEnterFullScreen:(NSNotification *)notification
{

}

- (void)windowWillExitFullScreen:(NSNotification *)notification
{

}

I proceeded by going through the description dictionaries and checking if the current cursor position was inside the bounds of the windows. 我先通过描述字典,然后检查当前光标位置是否在窗口范围内。 The first window to satisfy this would be the window right under the cursor, which is exactly what I needed. 满足此要求的第一个窗口就是光标正下方的窗口,这正是我所需要的。

Separately, to find the current top-most window, I used the iChat Apple example of the Accessibility API to register ApplicationActivatedNotification and MainWindowDidChangeNotification s. 另外,为了找到当前最顶部的窗口,我使用了Accessibility API的iChat Apple示例来注册ApplicationActivatedNotificationMainWindowDidChangeNotification Both notifications combined would let me keep track of the main window of the active app (top-most). 这两个通知的组合将使我能够跟踪活动应用程序的主窗口(最顶部)。 To get the bounds in this case, I just got the main window's position and size using the Accessibility API. 在这种情况下,为了获得界限,我只是使用Accessibility API获得了主窗口的位置和大小。

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

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