Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoa: Key down event on NSView not firing

I have made a custom NSView and have implemented the keyDown: method. However, when I press keys the method is never called. Do I have to register to receive those events? fyi, I am making a document based application and can handle this code anywhere (doesn't have to be in this view). What is the best place to do this in a document based application such that the event will occur throughout the entire application?

like image 831
mtmurdock Avatar asked Apr 05 '12 01:04

mtmurdock


2 Answers

You need to override -acceptsFirstResponder to return YES.

like image 185
Ken Thomases Avatar answered Nov 14 '22 23:11

Ken Thomases


In Swift:

class MDView: NSView {
    override var acceptsFirstResponder: Bool { return true }
}
like image 10
Matt Darby Avatar answered Nov 15 '22 01:11

Matt Darby