This can be done using the IOKit and HIDManager classes.
If exclusive access to the keyboard is required, you can use the kIOHIDOptionsTypeSeizeDevice
parameter, but the program must be run with root privileges.
Below is the code snippet needed to get this result:
// Create a manager instance IOHIDManagerRef manager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDManagerOptionNone); if (CFGetTypeID(manager) != IOHIDManagerGetTypeID()) { exit(1); } // Setup device filtering using IOHIDManagerSetDeviceMatching //matchingdict = ... IOHIDManagerSetDeviceMatching(manager, matchingdict); // Setup callbacks IOHIDManagerRegisterDeviceMatchingCallback(manager, Handle_DeviceMatchingCallback, null); IOHIDManagerRegisterDeviceRemovalCallback(manager, Handle_RemovalCallback, null); IOHIDManagerRegisterInputValueCallback(manager, Handle_InputCallback, null); // Open the manager and schedule it with the run loop IOHIDManagerOpen(manager, kIOHIDOptionsTypeSeizeDevice); IOHIDManagerScheduleWithRunLoop(manager, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); // Start the run loop //...
More details can be found in Apple docs here: http://developer.apple.com/library/mac/#documentation/DeviceDrivers/Conceptual/HID/new_api_10_5/tn2187.html
The full code that I used for my application can be found here: https://gist.github.com/3783042
Garetjax
source share