简体   繁体   中英

Cast NSWindow to NSPanel

I am accessing a Virtual keyboard (external application - in Adobe Flex). I want that keyboard should be non focusable. So I have to apply

styleMask:NSNonactivatingPanelMask 

But am accessing the keyboard as

NSWindow *myMainWindow = [[[NSApplication sharedApplication] windows] objectAtIndex:0];

as NSNonactivatingPanelMask can only be applied to the NSPanel only

If I can type cast the NSWindow to NSPanel (?) then it possible.

----------My Previous question------------

Return focus to Editor after clicking a button in floating window of MAC

An NSPanel is an NSWindow , the reverse is not true - inheritance doesn't work both ways!

Furthermore casting an object reference from one type to another does not change the actual type of the reference object, so even if you cast an A * to a B * then invoking a method gets you exactly the same method as without the cast - the cast serves to inform the compiler that you know the actual object referenced is a different type and so quietens the compiler when you invoke a B method.

Even if you could get past all that, you state you want the keyboard to be non-focusable, which is not the same as non-activating - the former is about being an applications main window , the latter is about accepting input without activating an application.

The main window of an application is the one which is focussed, its frame highlighted in some way, etc. The key window of an application is the one which is accepting user input. They are often the same window, but need not be. It sounds like you want your keyboard to by the key window without being the main window - ie behave like a panel.

NSWindow has methods canBecomeMainWindow and canBecomeKeyWindow which determine whether a window can become main or key respectively. While you cannot change what these return for an NSWindow instance you can subclass NSWindow and override these methods - this is what NSPanel does - see the NSWindow documentation for these methods. So if you, say, define KeyboardWindow as an NSWindow subclass and override canBecomeMainWindow to return NO . Do this and you have a window which will not become main (focussed) but can accept input.

HTH

AS provided by @ashirbad

NSWindow *mainWindow = [[NSApp windows] objectAtIndex: 0];
NSView *mainContentView = [mainWindow contentView];

NSPanel *mainPanel = [[NSPanel alloc] initWithContentRect:[mainContentView frame]
             styleMask:NSBorderlessWindowMask | NSNonactivatingPanelMask
            backing:NSBackingStoreBuffered defer:YES];

[mainPanel setContentView:mainContentView];
[mainPanel setLevel:NSScreenSaverWindowLevel];

[mainPanel makeKeyAndOrderFront:nil];
[mainContentView orderOut:nil]; //error

This solved my problem. But I'm getting an error orderOut is not defined in NSView.

If the top app is a Mac application then it's ok But if it is a flex app (in this case). The application not responding at this line.

[mainPanel setContentView:mainContentView];

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