title: OS X: Detect system-wide keyDown events?
body: <p>I'm working on a typing-tutor application for Mac OS X that needs to have keystrokes forwarded to it, even when the application is not in focus.</p>
  
  <p>Is there a way to have the system forward keystrokes to the app, possibly through NSDistributedNotificationCenter? I've googled myself silly, and haven't been able to find an answer...</p>
  
  <p><strong>EDIT:</strong> <em>Sample code below</em>.</p>
  
  <p>Thanks @NSGod for pointing me in the right direction -- I ended up adding a <a href="https://developer.apple.com/documentation/appkit/nsevent/1535472-addglobalmonitorforeventsmatchin" rel="noreferrer">global events monitor</a> using the method <code>addGlobalMonitorForEventsMatchingMask:handler:</code>, which works beautifully. For completeness, my implementation looks like this:</p>
  
  <pre><code>// register for keys throughout the device...
  [NSEvent addGlobalMonitorForEventsMatchingMask:NSKeyDownMask
                                         handler:^(NSEvent *event){
  
      NSString *chars = [[event characters] lowercaseString];
      unichar character = [chars characterAtIndex:0];
  
      NSLog(@"keydown globally! Which key? This key: %c", character);
  
  }];
  </code></pre>
  
  <p>For me, the tricky part was using blocks, so I'll give a little description in case it helps anyone:</p>
  
  <p>The thing to notice about the above code is that it's all <strong>one single method call</strong> on NSEvent. The block is supplied as an argument, directly <strong>to</strong> the function. You could think of it kind of like an inline delegate method. Just because this took a while to sink in for me, I'm going to work through it step by step here:</p>
  
  <pre><code>[NSEvent addGlobalMonitorForEventsMatchingMask:NSKeyDownMask
  </code></pre>
  
  <p>This first bit is no problem. You're calling a class method on NSEvent, and telling it which event you're looking to monitor, in this case NSKeyDownMask. A <a href="http://developer.apple.com/library/mac/#DOCUMENTATION/Cocoa/Reference/ApplicationKit/Classes/NSEvent_Class/Reference/Reference.html%23//apple_ref/occ/clm/NSEvent/addGlobalMonitorForEventsMatchingMask:handler:" rel="noreferrer">list of masks for supported event types</a> can be found <a href="http://developer.apple.com/library/mac/#DOCUMENTATION/Cocoa/Reference/ApplicationKit/Classes/NSEvent_Class/Reference/Reference.html%23//apple_ref/occ/clm/NSEvent/addGlobalMonitorForEventsMatchingMask:handler:" rel="noreferrer">here</a>.</p>
  
  <p>Now, we come to the tricky part: handler, which expects a block:</p>
  
  <pre><code>handler:^(NSEvent *event){
  </code></pre>
  
  <p>It took me a few compile errors to get this right, but (thank you Apple) they were very constructive error messages. The first thing to notice is the carat ^. That signals the start of the block. After that, within the parentheses, </p>
  
  <pre><code>NSEvent *event
  </code></pre>
  
  <p>Which declares the variable that you'll be using within the block to capture the event. You could call it </p>
  
  <pre><code>NSEvent *someCustomNameForAnEvent
  </code></pre>
  
  <p>doesn't matter, you'll just be using that name within the block. Then, that's just about all there is to it. Make sure to close your curly brace, and bracket to finish the method call:</p>
  
  <pre><code>}];
  </code></pre>
  
  <p>And you're done! This really is kind of a 'one-liner'. It doesn't matter where you execute this call within your app -- I do it in the AppDelegate's applicationDidFinishLaunching method. Then, within the block, you can call other methods from within your app.</p>