Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
969 views
in Technique[技术] by (71.8m points)

macos - OSX HID Filter for Secondary Keyboard?

I would like to filter keyboard input on a second keyboard, and prevent the key events for that second keyboard from reaching the OS (handle them myself). How can this be done?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It can be done by using IOKit and the HIDManager class.

If exclusive access to the keyboard is desired, the kIOHIDOptionsTypeSeizeDevice option can be used, but the program will have to be run with root privileges.

A stub of the code required to obtain this result is shown below:

// 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 detailed information can be found in the Apple docs over here: http://developer.apple.com/library/mac/#documentation/DeviceDrivers/Conceptual/HID/new_api_10_5/tn2187.html

The complete code I used for my application can be found here: https://gist.github.com/3783042


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...