0

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).

Community
  • 1
  • 1
Nicolas Miari
  • 16,006
  • 8
  • 81
  • 189
  • `-keyDown:` is a method of the `NSResponder` class. I wish to toggle certain flag depending on the status of the shift key. – Nicolas Miari Mar 25 '16 at 06:51
  • If you say 'key down' under the hood of desktop applications, one may be reminded of control:(NSControl *)control textView:(NSTextView *)inputfield doCommandBySelector:(SEL)commandSelector – El Tomato Mar 25 '16 at 06:53
  • Why do you write mouseDown in NSClipView? – El Tomato Mar 25 '16 at 06:57
  • I said "keyDown:". Still can't see the words "key down" in `control:(NSControl *)control textView:(NSTextView *)inputfield doCommandBySelector:(SEL)commandSelector`. – Nicolas Miari Mar 25 '16 at 07:01
  • My clip view does some custom drawing: A "marching ants" rectangle or ellipse of the dragged region. I wanted to use the shift key to toggle between "clip selection to content view" (e.g., the borders of the image displayed inside the scrollview) or not. The mouse event handlers seem to work with the clip view; the keyboard ones not. – Nicolas Miari Mar 25 '16 at 07:02
  • Look, why do you make such a big deal between keyDown and key down? Your question concerns mouse down, not keyDown or key down or Keydown. – El Tomato Mar 25 '16 at 07:06
  • No. My question is bout the method keyDown: not being called. mouse events work fine. Please read the question again. Although the exact method name did not appear in the original text of the question (but in the title), I never mentioned anything about text views either. – Nicolas Miari Mar 25 '16 at 07:11
  • 1
    I don't know why you subclassed `NSClipView` as you are supposed to embed your custom view within an `NSScrollView` and work from there. I think this is the root cause of your issue. – trojanfoe Mar 25 '16 at 08:43
  • Perhaps you are right. At first I subclassed `NSClipView` in order to keep the content centered even when it is way smaller than the scroll view's bounds (like Preview does). Then, the mouse drag/selection thing came up, and I opted not to make a dedicated NSView subclass and keep everything in the clip view. I am still curious as to why it is only the key events that fail. – Nicolas Miari Mar 25 '16 at 08:47
  • Keydown is sent to the first responder and the responder chain and the clipview is not in the responder chain. Mousedown is sent to the view returned from `hitTest:`. – Willeke Mar 25 '16 at 13:26

0 Answers0