简体   繁体   中英

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. 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:

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

And the same with nil instead of self . Any ideas?


Edit 1: I've tried to set the properties like this and this works but destroys the HUD-style:

[_window setStyleMask:NSHUDWindowMask | NSNonactivatingPanelMask];

If I do this the HUD-style is there again but also the window becomes key again:

[_window setStyleMask:NSHUDWindowMask | NSTitledWindowMask | NSNonactivatingPanelMask];

Edit 2: Now I've also tried subclassing and added the following code to KeyboardPanel.m:

- (BOOL)canBecomeKeyWindow {
    return NO;
}

Now NSLog(@"Can become key window: %hhd", _window.canBecomeKeyWindow) returns 0 , but the panel becomes key nevertheless...

Use an NSPanel and set its becomesKeyOnlyIfNeeded property to true. You probably also want to use NSNonactivatingPanelMask in the style mask for the panel.


Update:

Since you never want to let the panel become key, you can simply use a custom subclass which overrides -canBecomeKeyWindow to return false:

- (BOOL) canBecomeKeyWindow
{
    return NO;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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