2
votes

I'm having a weird issue where I click away from my window. It resigns main and key. Then I click back to it and the view that was clicked on does not respond to the mouse click. But the second click does cause the view to respond. As if during the first click, the window wasn't key and the click caused it to become key and was able to handle the second click.

I subclassed NSApplication and intercepted sendEvent: to see if my application was getting the events during the first click event and indeed it was. Furthermore, I dumped the responder chain and saw that indeed my view was in the chain, but the window was not key until the second click. I also noticed that when I receive the first click event, the application isn't even active. How is this possible? Isn't an application supposed to become active when it receives a mouse event? Chicken/egg...

Any thoughts/sugggestions?

Update: After reading through the docs I found this:

Mouse-down events are sent when a user presses the mouse button while the cursor is over a view object. If the window containing the view is not the key window, the window becomes the key window and discards the mouse-down event. However, a view can circumvent this default behavior by overriding the acceptsFirstMouse: method of NSView to return YES.

So that appears to be what is happening. However, I tried overriding acceptsFirstMouse: and acceptsFirstResponder but to no avail. My views are still not "accepting first mouse".

Thanks!

1
Btw, my application policy is NSApplicationActivationPolicyAccessory..Larvell Jones
Also, preventWindowOrdering never gets called.. Still digging..Larvell Jones
the "become active" behaviour of your app is maybe overriden by your subclass, maybe you missed calling [super something]; somewhere ?user971401
Thanks for the reply. Yeah there were a few cases where I was overriding mouseDown: and not calling the super. Still not accepting first mouse though. When I dump the responder chain, I made it so every view in the chain acceptsFirstMouse:. However I set a symbolic breakpoint on -[NSView acceptsFirstMouse:] and NSApplication is asking other views if they accept first mouse. Shouldn't it be asking the views in the responder chain? Confused.Larvell Jones
I never subclassed NSApplication myself, so it was just a guess. Yes the event goes through the responder chain, passing hitTest: for each, but I'm sure you already tested that your view objects recieved the events in mouseDown:; at least by clicking almost anywhere...user971401

1 Answers

0
votes

in 10.10 > in your app delegate - you can hook into the mouse events then force the window to become active.

- (void)applicationDidUpdate:(NSNotification *)notification {
     NSLog(@"did update");
    // [[NSApp mainWindow] makeKeyWindow];doesn't work. not sure why. 
    [[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
}

- (void)applicationDidResignActive:(NSNotification *)notification {
   // your window lost focus here
}

- (void)applicationDidBecomeActive:(NSNotification *)notification {
   // 
}