简体   繁体   English

手动使NSPanel / NSWindow退出密钥或不成为密钥

[英]Make NSPanel/NSWindow resign key manually or not becoming key

I'm trying to create a virtual keyboard in Objective-C/Cocoa/OS X. The keyboard-window is a HUD-panel. 我正在尝试在Objective-C / Cocoa / OS X中创建虚拟键盘。键盘窗口是HUD面板。 How can I make that the panel doesn't become key if I click on the keyboard? 如果我单击键盘,如何使面板不成为键?

I already tried the following approach in the - (void)windowDidBecomeKey:(NSNotification *)notification -method (which get's called), but it doesn't work: 我已经在- (void)windowDidBecomeKey:(NSNotification *)notification -method(被称为)中尝试了以下方法,但是它不起作用:

NSDisableScreenUpdates();
[_window orderOut:self];
[_window orderFront:self]; // Not asking it to be key
NSEnableScreenUpdates();

And the same with nil instead of self . 并且用nil代替self也是一样。 Any ideas? 有任何想法吗?


Edit 1: I've tried to set the properties like this and this works but destroys the HUD-style: 编辑1:我试图设置像这样的属性,这可以但破坏了HUD样式:

[_window setStyleMask:NSHUDWindowMask | NSNonactivatingPanelMask];

If I do this the HUD-style is there again but also the window becomes key again: 如果执行此操作,则将再次出现HUD样式,但窗口也会再次成为关键:

[_window setStyleMask:NSHUDWindowMask | NSTitledWindowMask | NSNonactivatingPanelMask];

Edit 2: Now I've also tried subclassing and added the following code to KeyboardPanel.m: 编辑2:现在我也尝试了子类化,并将以下代码添加到KeyboardPanel.m:

- (BOOL)canBecomeKeyWindow {
    return NO;
}

Now NSLog(@"Can become key window: %hhd", _window.canBecomeKeyWindow) returns 0 , but the panel becomes key nevertheless... 现在NSLog(@"Can become key window: %hhd", _window.canBecomeKeyWindow)返回0 ,但是面板仍然变为键...

Use an NSPanel and set its becomesKeyOnlyIfNeeded property to true. 使用一个NSPanel并将其设置为becomesKeyOnlyIfNeeded属性为true。 You probably also want to use NSNonactivatingPanelMask in the style mask for the panel. 您可能NSNonactivatingPanelMask在面板的样式蒙版中使用NSNonactivatingPanelMask


Update: 更新:

Since you never want to let the panel become key, you can simply use a custom subclass which overrides -canBecomeKeyWindow to return false: 由于您永远不想让面板成为键,因此您可以简单地使用覆盖-canBecomeKeyWindow的自定义子类来返回false:

- (BOOL) canBecomeKeyWindow
{
    return NO;
}

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

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