NSTextField in status bar doesn't want to receive focus - objective-c

For some reson sometimes a NSTextField I'm using in Status Bar menu doesn't always allow me to input text. I click it and nothing happens as if it was disabled. Upon restarting program it works again. I don't do anything with it, it's just created in the interface builder.

That's because no NSWindow contains the NSTextField. The NSWindow sets the first responder when the window gets the main window. The NSStatusBar is global. It's never focused so your textfield only will be focused in the very beginning.
I'm not sure if there's a way to solve this problem in a nice way. You might try to set the first responder manually. You could also add a global event monitor
Example:
[NSEvent addGlobalMonitorForEventsMatchingMask:NSKeyDownMask handler:^(NSEvent* incoming) {
[textfield setStringValue:[incoming characters]];
}];
Note: This is a very bad way to fix this problem. I'd first try to set the NSTextField manually as a first responder if this is possible.

Related

NSTextField click-through?

I have a static NSTextField that overlays a large error message in my OS X app. I'm trying to get it to allow the user to click controls beneath it.
In IB I've unchecked "enabled" and I've checked "Refuses First Responder"
I've also done it in code because that wasn't working:
[largeErrorText setEnabled:NO];
[largeErrorText setRefusesFirstResponder:YES];
Still, it is getting in the way of interacting with the objects below it. Any ideas what else it might be?
The only way I have found to make an object transparent to the click is to subclass that object (in your case the NSTextField) and override the hitTest method returning nil. This way that NSTextField will not respond to the click so the NSView below will respond to the click.
- (NSView*)hitTest:(NSPoint)aPoint
{
return nil;
}
I assume you are describing a scenario like the following image shows:
The inner red rectangle is the frame outline of the NSTextField label, and you're saying that even though you've disabled the text field and set refuses first responder, your clicks do not go through to the NSButton?
This design scenario describes a condition called "Overlapping sibling views". I would generally try to avoid this if at all possible. If you can't, you can get the desired behavior by making sure that the NSTextField label is "behind" all of the other UI objects that you want to be able to interact with. You can do that by selecting the label and choosing Editor > Arrange > Send to Back. That will assure that the button is in front of the text field so that it can properly intercept mouse events.

Invoke NSTextField's action to be send when clicked ouside

I have a NSTextField that have been edited. If I press enter then action is being called as expected.
I would like to have the same effect when I click anywhere outside of NSTextField. Resigning First Responder could be the key, but that happens only when some elements are clicked/selected/focused (e.g. another NSTextField or NSTableView) and doesn't happen with others (e.g. NSButton or NSSlider).
NSTextField focus can be unset using [[self window] makeFirstResponder:nil] but that would be too ugly to call this line in every object's action.
Is there a better solution?
You will want to delve into the Cocoa Event Handling Guide.
Take a good look at NSResponder.
Many objects inherit from NSResponder, including NSWindow and NSApplication.
Taking a guess at what you are trying to accomplish, committing an edit when the view is not in focus, you may want to also include observing notifications when the app will terminate, go into the background, etc...

How to force an NSWindow to be always active/focused?

I have a transparent NSWindow that follows the user's screen everywhere he goes (the NSWindowstays in front of every app, no matter what, even fullscreen apps).
In that NSWindow i have a mouseDown event that shows a popup. Let's say i'm on safari in fullscreen mode and i have my Window in front of it, i click on safari and i click again on my Window: nothing happens, the mouseDown doesn't occur. I have to click again so the mouseDown event is triggered.
How can i force my NSWindow to be always active so i don't have to click it 2x to trigger the mouseDown when i click on a background app and click in my window again?
Thank you!
I'm not sure if this is exactly what you want (it's not quite a window wide setting), but, from the documentation:
By default, a mouse-down event in a window that isn’t the key window
simply brings the window forward and makes it key; the event isn’t
sent to the NSView object over which the mouse click occurs. The
NSView can claim an initial mouse-down event, however, by overriding
acceptsFirstMouse: to return YES.
The argument of this method is the
mouse-down event that occurred in the non-key window, which the view
object can examine to determine whether it wants to receive the mouse
event and potentially become first responder. You want the default
behavior of this method in, for example, a control that affects the
selected object in a window.
However, in certain cases it’s
appropriate to override this behavior, such as for controls that
should receive mouseDown: messages even when the window is inactive.
Examples of controls that support this click-through behavior are the
title-bar buttons of a window.
Or you could try fiddling with
- (void)sendEvent:(NSEvent *)theEvent
and see if you can handle events in a custom way.
If you add a borderless NSButton instance to your window's view and set your image as the button's image (and as its alternate image, to make it more beautiful), it will work out of the box: Just connect the button's action method to your app delegate (or the object where you want to process the click action). A click on the image (i.e. the button) will then trigger the button's action method, no matter which window is active.
This worked for me, hope that will be helpful, This will keep your window always on Top of all applications
[self.window makeKeyAndOrderFront:nil];
[self.window setLevel:NSStatusWindowLevel];
I think what you really should do is use an NSPanel (a floating palette -- a special kind of NSWindow) that will do exactly what you want in a way that's consistent with the OS rather than trying to fight intended behavior.
Here's the NSPanel documentation:
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/nspanel_Class/Reference/Reference.html
And here's some helpful and pithy information:
http://cocoadev.com/wiki/NSPanel
By default, an NSPanel will disappear when the application is inactive, but you can turn this off.
I apologize for not laying it out more fully ... pressed for time.
Edit:
Note that you can probably get your window to behave as desired simply:
"The NSView can claim an initial mouse-down event, however, by overriding acceptsFirstMouse: to return YES."
https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/EventOverview/HandlingMouseEvents/HandlingMouseEvents.html
You'll need to do this with any NSView subclass to skip the "activation click".

NSTextField inside NSPopover is not key until mouse click

I've got an NSPopover that is shown from interaction with an NSStatusItem. I've blogged about the hacks I needed to do to make input even possible in this situation here: http://blog.brokenrobotllc.com/using-nspopover-with-nsstatusitem
I have an NSTextField inside the NSPopover's content view. When I open the NSPopover, the NSTextField appears as if it is key (the cursor blinks). But, when typing, nothing shows up. If I click the mouse in the field, my input starts showing up there.
I've tried things like invoking NSWindow's makeFirstResponder upon popoverDidShow:. There was no change in behavior from this. Anyone have any ideas here?
My guess is you need to make your app active; try calling
[NSApp activateIgnoringOtherApps:YES];
when you show your popover.
Edit: Of course, I could be wrong. This is all just off the top of my head.

Problem avoiding first responder on a NSTextField

I need to change behavior of input fields in a really simple app:
Whenever i launch the application the first text field get the focus, but i don't want this behavior.
I tried checking "Refuses first responder" in IB. It works but with this option checked i can't move between input fields pressing "tab" button.
What can i do to avoid focus at startup and keep the ability to move with tab keyboard button ?
The (previously) accepted answer isn't reliable and doesn't work very well. The other answer with the hidden NSTextField isn't very great either because now you have a new element in your Tab order.
The solution I've found works best so far is:
Make the NSTextField refusesFirstResponder YES on app launch.
Then, in viewDidAppear for the controller, go ahead and set refusesFirstResponder back to NO.
Everything behaves perfect after launch, and I don't have a greedy NSTextField stealing first responder on app startup.
I found the solution, you can add [window makeFirstResponder:nil]; after awakeFromNib for example in applicationDidfinishLaunching.
window?.makeFirstResponder(nil) does not work for me - when I check who is the first responder, it is the window (and not a NSTextField) but still, the first NSTextField is selected and active. The solution for me (though I know not the cleanest one) was to create a hidden text field and make it the first responder every time the window did load.
window?.makeFirstResponder(nil) worked only when I set all NSTextFields to RefuseFirstResponder, but then using Tab to switch between them of course do not work.
This worked for me,
override func viewDidAppear() {
Timer.scheduledTimer(withTimeInterval: 0.1, repeats: false) { [weak self] (timer) in
NSApplication.shared.windows[0].makeFirstResponder(self?.textUsername)
let tRange = self?.textUsername.currentEditor()?.selectedRange
self?.textUsername.currentEditor()?.selectedRange = NSMakeRange((tRange?.length)!, 0)
}
}