tags:

views:

753

answers:

2

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?

+3  A: 

Hi Kevin,

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.

Dirk Stoop
This is great! The keyboard posts a HID event just before the CGEvent gets to the tap, so I can correlate the data. Thanks for the tip.
kevingessner
I'm glad it worked, don't forget to thank Dave :)
Dirk Stoop
+2  A: 

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

Wil Shipley
It appears that version 1.1 includes some code from Delicious Monster. And not being able to intercept events will actually be fine - I can use the tap to intercept events, and just use the HID event to learn about the keyboard.
kevingessner