问题
My application uses an event tap to capture keyboard events, and I'd like to know which device (i.e. which keyboard) each event comes from. Is there an sort of device-identifying information along with the CGEvent that a tap gets? I've looked at NSEvent's methods, and the various CGEventField keys, but none of them seem to be device-unique. Any help?
回答1:
You might want to take a look at DDHidLib, Dave Dribin's excellent framework to work with USB HID devices independently.
http://www.dribin.org/dave/blog/archives/2007/03/19/ddhidlib_10
(not just about joysticks, so read more than the first paragraph of that blog post)
Some of the functionality of DDHidLib no longer works under Leopard, due to some security concerns at Apple regarding capturing an HID device, but if you're lucky it might provide you with what you need.
回答2:
DDHidLib is neat, and in fact I rewrote parts of it for Delicious Library 2 for Leopard's newer HID APIs, and submitted the changes back to the original author -- if you write him you can get the Leopard-only sample code.
Unfortunately, the new Leopard HID APIs have the ability to peak at keyboard events as they pass by, but NOT to intercept them, so you can't build your own application-level device handler unless it's OK that the key events are also going to the AppKit, as well. (This is why there's a BONKING noise when you use a USB barcode scanner in Delicious Library 2 - I peak at the scanner and read the barcode, but then the typing is still sent to the topmost window, which doesn't want it, and beeps a lot. Sigh.)
-Wil
来源:https://stackoverflow.com/questions/219623/device-information-from-nsevent-cgevent