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?
            Asked
            
        
        
            Active
            
        
            Viewed 1.1k times
        
    19
            
            
         
    
    
        mtmurdock
        
- 12,756
- 21
- 65
- 108
2 Answers
30
            You need to override -acceptsFirstResponder to return YES.
 
    
    
        Ken Thomases
        
- 88,520
- 7
- 116
- 154
- 
                    1I have the same problem as the asker (custom NSView in a document-based application), and just overrided acceptsFirstResponder but keyDown is still not triggered. If you have any other advice as to what it could be I would be eternally grateful! – chm Oct 03 '12 at 06:40
- 
                    1Do you have reason to believe that your view is the window's first responder? You can set the `initialFirstResponder` in code before the window is ordered on screen or connect it in IB. At other times, you can use `-makeFirstResponder:`. Also, make sure your window can become key. – Ken Thomases Oct 04 '12 at 23:07
10
            
            
        In Swift:
class MDView: NSView {
    override var acceptsFirstResponder: Bool { return true }
}
 
    
    
        Matt Darby
        
- 6,294
- 4
- 27
- 35