OSX HID filter for optional keyboard?

I would like to filter out keyboard input on the second keyboard and prevent key events for this second keyboard from reaching the OS (handle them yourself). How can I do that?

+7
source share
2 answers

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

+5
source

I'm going to take a hit, but not write my own driver, you cannot intercept the buffer. This is to prevent keyloggers and other malicious programs. Everything should go through the OS.

-one
source

All Articles