set NSWindow focused
Asked Answered
I

2

7

I have an app winth one window and one panel, attached to this window.

steps:

  1. deactivate my app (app opened, but without the focus)
  2. click on a button on panel (panel is focused now, but main window is not)

How to set focus to the main window (parent window) from the panel?

Initiation answered 23/2, 2011 at 10:24 Comment(4)
Is the main window a Cocoa main window? Is your application NSDocument-based? Do you mean key window status when you say focus?Obannon
When I say "to set focus", I mean "to activate" main window, set key window status. Application is always on top of the screen. And when I click somwhere outside the main window the application deactivating, but placed on top.Initiation
Your answer with activateWithOptions: worked for me too. If you post it as an answer I'd be happy to upvote it :)Savor
Make sure your panel responds YES to canBecomeKeyWindowJefferey
O
10

It is not clear what you mean by focus, and whether what you call main window is a main window as defined in Cocoa. Assuming it is a Cocoa main window and focus is the same as key status,

[[NSApp mainWindow] makeKeyWindow];

or

[[NSApp mainWindow] makeKeyAndOrderFront:self];

If it is not a Cocoa main window, you need to have a reference to it and send it -makeKeyWindow or -makeKeyAndOrderFront:.

Obannon answered 23/2, 2011 at 10:44 Comment(6)
its not working. Maybe, because main window is NSFloatingWindowLevel?Initiation
@Initiation Does the main window have a title bar or a resize bar? If you send -canBecomeKeyWindow to the main window, what is the return value?Obannon
-canBecomeKeyWindow returns trueInitiation
I understand what I need: [[NSRunningApplication currentApplication] activateWithOptions: NSApplicationActivateIgnoringOtherApps]; But I compiling my apps for 10.6 and 10.5. NSRunningApplication class is only in 10.6. How to do that in 10.5?Initiation
I don't know why, but [NSApp deactivate]; do what I need :)Initiation
[[NSApplication sharedApplication] activateIgnoringOtherApps:YES]; // "Available in OS X v10.0 and later."Ballistic
S
13

Swift 5 version of @BUDDAx2 answer:

NSApplication.shared.activate(ignoringOtherApps: true)
Spicer answered 16/11, 2019 at 19:38 Comment(0)
O
10

It is not clear what you mean by focus, and whether what you call main window is a main window as defined in Cocoa. Assuming it is a Cocoa main window and focus is the same as key status,

[[NSApp mainWindow] makeKeyWindow];

or

[[NSApp mainWindow] makeKeyAndOrderFront:self];

If it is not a Cocoa main window, you need to have a reference to it and send it -makeKeyWindow or -makeKeyAndOrderFront:.

Obannon answered 23/2, 2011 at 10:44 Comment(6)
its not working. Maybe, because main window is NSFloatingWindowLevel?Initiation
@Initiation Does the main window have a title bar or a resize bar? If you send -canBecomeKeyWindow to the main window, what is the return value?Obannon
-canBecomeKeyWindow returns trueInitiation
I understand what I need: [[NSRunningApplication currentApplication] activateWithOptions: NSApplicationActivateIgnoringOtherApps]; But I compiling my apps for 10.6 and 10.5. NSRunningApplication class is only in 10.6. How to do that in 10.5?Initiation
I don't know why, but [NSApp deactivate]; do what I need :)Initiation
[[NSApplication sharedApplication] activateIgnoringOtherApps:YES]; // "Available in OS X v10.0 and later."Ballistic

© 2022 - 2024 — McMap. All rights reserved.