Passing keyboard events from NSTextView to another

Viewed 54

I have NSTextView which pops up a NSPopOver that contains another NSTextView. In some cases, I'd like to pass the keyboard events (arrow keys, mostly) from the "top" text view to the bottom one.

I have overridden keyDown on the topmost text view and tried various ways of forwarding the keyboard event to the view below.

Just calling bottomTextView.keyDown(with: event) doesn't do anything. The closest I've gotten is using window.sendEvent(event).

override func keyDown(with event: NSEvent) {
    if (NSLocationInRange(Int(event.keyCode), NSMakeRange(123, 4)) &&
        self.string.count == 0 && event.modifierFlags.contains(.shift)) {
        // Pass the event through somehow
        return
    }
    
    super.keyDown(with: event)
}

Through trial and error, I found one way which actually did pass on the events: calling the methods twice.

// Make the bottom text field first responder
self.window?.makeFirstResponder(self.delegate.bottomTextView)
self.window?.makeFirstResponder(self.delegate.bottomTextView)
// Send the keydown event to window
self.window?.sendEvent(event)
self.window?.sendEvent(event)

However, this causes strange issues, such as the topmost text view getting a one-byte string as its contents.

Is there a correct way to actually forward any key events from a text view to another, or is it even possible?

0 Answers
Related