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

-------------Problems Reply------------

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;
}

Category:objective c Views:0 Time:2018-04-14

Related post

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.239 (s). 11 q(s)