My app is not document based, and its sole window is managed by a custom, xib-based NSWindowController
subclass that I instantiate within the app delegate code:
- (void) applicationDidFinishLaunching:(NSNotification*) aNotification
{
_mainWindowController = [MainWindowController new];
// (stored in ivar just to prevent deallocation)
//[_mainWindowController showWindow:self];
// ↕︎ Not sure about the difference between these two... both seem to work.
[[_mainWindowController window] makeKeyAndOrderFront:self];
}
I have subclassed NSClipView
to "center content inside a scroll view" (instead of having it pegged to the lower left corner) when it is zoomed to a size smaller than the clip view, and also implement custom functionality on mouse drag etc.
My window does have a title bar.
My window isn't borderless (I think), so I am not subclassing NSWindow
.
I have overriden -acceptsFirstResponder
, -canBecomeKeyView
and -becomeFirstResponder
in my NSClipview
subclass (all return YES
).
The drag events do trigger -mouseDown:
etc., and if I set a breakpoint there, the first responder at that point is the same as the window hosting my clip view: [self.window firstResponder]
and [self window]
give the same memory address.
What am I missing?
Update
I put together a minimal project reproducing my setup.
I discovered that if my custom view is the window's main view, -keyDown:
is called without problems. But if I place a scroll view and replace its clip view by my custom view (to do that, I need to change the base class from NSView
to NSClipView
, of course!), -keyDown:
is no longer triggered.
I assume it has something to do with how NSScrollView
manages events (however, as I said before, -mouseDown:
, -mouseDragged:
etc. seem to be unaffected).
I also discovered that I can override -keyDown:
in my window controller, and that seems to work, so I have decided to do just that (still open to an answer, though). Also, since I'm trying to detect the shift key alone (not as a modifier of another key), I'd rather use:
- (void) flagsChanged:(NSEvent *) event
{
if ([event modifierFlags] & NSShiftKeyMask) {
// Shift key is DOWN
}
else{
// Shift key is UP
}
}
...instead of -keyDown:
/ -keyUp:
(taken from this answer).