macos 使 NSWindow 位于前面但不处于焦点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4859974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 07:40:40  来源:igfitidea点击:

Make NSWindow Front But Not In Focus

objective-ccocoamacos

提问by redhotvengeance

I'm looking to bring a new NSWindow in front of all other windows, but not have it take focus.

我希望在所有其他窗口前面引入一个新的 NSWindow,但没有让它成为焦点。

I can make it appear in front with focus with the following:

我可以通过以下方式让它出现在焦点前面:

NSApplication *thisApp = [NSApplication sharedApplication];
[thisApp activateIgnoringOtherApps:YES];

[self makeKeyAndOrderFront:self];

Any clues on how to make it appear on top but not take focus away from another application?

关于如何使其出现在顶部但不将注意力从另一个应用程序移开的任何线索?

采纳答案by cobbal

Instead of makeKeyAndOrderFront:, try just orderFront:(docs)

而不是makeKeyAndOrderFront:,只尝试orderFront:文档

回答by NSGod

Try something like this:

尝试这样的事情:

[window setLevel:NSScreenSaverWindowLevel + 1];
[window orderFront:nil];

This will show the window above other application's windows, but without making it active. A window with a normal window level in application A cannot be shown in front of a window of application B, if application B is the active application. (There is good reason for this, btw).

这将在其他应用程序的窗口上方显示该窗口,但不会使其处于活动状态。如果应用程序 B 是活动应用程序,则应用程序 A 中具有正常窗口级别的窗口不能显示在应用程序 B 的窗口前面。(顺便说一句,这是有充分理由的)。

Please use this method with discretion. In many cases, it will likely violate the human interface guidelines. If misused, it can have a tendency to piss a user off. (For example, in my testing just now, the window appeared placed directly over the location I happened to be looking at in Safari. The fact that it was in the way of what I was doing, yet had the audacity to notbecome key, made it even more irritating. If it were up out of the way in a corner of my screen, it might be a different story).

请谨慎使用此方法。在许多情况下,它可能会违反人机界面指南。如果被滥用,它可能会激怒用户。(比如刚才在我的测试中,窗口出现在我在Safari中碰巧看到的位置的正上方。事实上,它妨碍了我正在做的事情,但有胆量成为关键,让它更烦人。如果它在我屏幕的一角不碍事,那可能是另一个故事)。

回答by zekel

The order front methods and level to the screensaver +1 didn't work for me. This answerfrom The-Kenny did, though:

屏幕保护程序 +1 的订单前台方法和级别对我不起作用。不过,The-Kenny 的回答确实如此:

[yourPanel setLevel:kCGMaximumWindowLevel];