tags:

views:

801

answers:

4

I'm looking for application-wide access to raw keyboard events in OS X, either using the Cocoa or Carbon frameworks (or any of the underlying APIs, for that matter). I know that I can override NSApplication's sendEvent: to get raw keyboard information, but for the meta keys (command, control, alternate, shift, etc) don't show up as keystroke events. I'm looking for something analogous to Microsoft's DirectInput framework.

Thanks!

A: 

You can use the RegisterHotKeyEvent Carbon function, I'm not sure if you can register for any of the meta keys explicitly though.

The blog post Program Global Hotkeys in Cocoa Easily explains how to do this.

Nick Haddad
+4  A: 

I think the equivalent to DirectInput is HID Manager. HID stands for "human interface device" and HID Manager (sometimes called HIDLib) is the low-level API to HIDs: keyboards, mice, and joysticks.

Leopard's got a new HID Manager API, documented in Technical Note TN2187. The pre-Leopard API is documented in HID Class Device Interface Guide. I wrote an Objecive-C wrapper around the older APIs, DDHidLib, which you may find useful. The Leopard API is much nicer. I'd use that directly, if you can.

Dave Dribin
+1  A: 

The Core Graphics framework also has some useful functionality buried in it as part of the remote operation system. Look for CGRemoteOperation.h, and check out the Quartz Events reference.

You can use the Quartz Events system to install application-specific or system-wide "event taps", which let you monitor and inject keyboard and mouse events at a pretty low level. A few years ago there were some bugs with application-specific event taps, but they've hopefully been worked out by now.

I think the HID stuff is mostly for driver development, so if you're just looking for a tool for your application, HID is probably overkill.

Greg
A: 

NSResponder derived classes have a method -(void)flagsChanged: that gets called when meta-keys are held down.

Walter