title: Swift 2: Detect Function Key Presses
body: <p>I'm writing a program in Swift 2 in Xcode 7 (for OS X 10.11) and I want it (this is the ViewController class) to be able to detect when the user presses a function key, especially when the app is in the background. I overrode <code>acceptsFirstResponder</code>:</p>
  
  <pre><code>override var acceptsFirstResponder: Bool { return true }
  </code></pre>
  
  <p>and I have implemented <code>keyUp:</code> and <code>keyDown:</code>:</p>
  
  <pre><code>override func keyDown(theEvent: NSEvent) {
      print("keyDown:\n")
      let ch: NSString = theEvent.charactersIgnoringModifiers!
      if ch.length == 1 {
          let char: Int = Int(ch.characterAtIndex(0))
          if char == NSF7FunctionKey {
              prevSong(NSNull())
          }else if char == NSF8FunctionKey {
              playPause(NSNull())
          }else if char == NSF9FunctionKey {
              nextSong(NSNull())
          }else{
              self.nextResponder?.keyDown(theEvent)
          }
      }else{
          self.nextResponder?.keyDown(theEvent)
      }
  }
  
  override func keyUp(theEvent: NSEvent) {
      print("keyUp:\n")
      self.nextResponder?.keyUp(theEvent)
  }
  </code></pre>
  
  <p>I originally had all the code in <code>keyUp:</code> instead but I never get "keyDown:" printed to the console. On a side note, if anyone knows a better way to detect function keys please say so.</p>
  
  <p>Why doesn't it detect key presses?</p>