In Swift keyDown(with event: NSEvent) not working when collectionView is selectable

247 views Asked by At

I have an NSCollectionView which lives inside an NSViewController. NSViewController overrides the following:

class myViewController : NSViewController{
    weak var collectionView : NSCollectionView!
    public override keyDown(with event: NSEvent){
         print("Key pressed.")
    } 
}

However, when collectionView.selectable = true, the view controller no longer receives key down events. I have tried this several ways, and in each case I either capture key too many key events (for example when the view controller is not even in focus) or too few (I don't get they key events at all). Please advise.

1

There are 1 answers

0
Nikunj Gangani On

KeyDown, keyUp, insertTab, insertText etc. This methods for keyboard click event.

mouseUp, mouseDown, mouseDragged, mouseMoved etc. This methods for mouse click event.

Your mouse event is fired when you click on any point in your application. And when you write text(A to Z, 0 to 9) in any textfield/textview while keyboard event is fired and ctrl, shift, capsLock, tab etc. click fired without write.