The Example Program

I’m going to make a simplified version of the HID Explorer program that is part of Apple’s sample code collection. The program shows a dialog box consisting of a pop-up menu and text fields. The pop-up menu contains the names of the HID devices connected to the user’s Mac. Selecting a device from the menu fills the text fields with the HID Manager data about the device.

Figure 1

There are two main classes for this program: InputController and InputDevice. The InputController class manages the input devices connected to the user’s machine.

@interface InputController : NSObject {
    NSMutableArrayPtr inputDeviceList;
    NSObject* currentlySelectedItem;

The inputDeviceList member contains the HID devices connected to the user’s Mac. The currentlySelectedItem member holds the device the user chooses from the pop-up menu. To get the bindings to work for a pop-up menu, you need a data member like currentlySelectedItem someplace in your program. If you’re using Core Data, a common technique is to add a variable of type NSManagedObject to the AppDelegate class.

The InputDevice class contains the HID data for an input device.

@interface InputDevice : NSObject {
    io_object_t deviceObject;
    NSStringPtr transportKey;
    long vendorID;
    long productID;
    long version;
    NSStringPtr manufacturer;
    NSStringPtr productName;
    NSStringPtr serialNumber;
    long usbLocationID;
    long usagePage;
    long usage;   

The important pieces of data are productName, usagePage, and usage. As you could probably tell, productName is the name of the device. The combination of usagePage and usage describes what the device is: keyboard, mouse, joystick, gamepad, or steering wheel.

I’ve included a finished project you can download. If you want to work through the example instead of using the finished project,

  1. Create a Cocoa application project.
  2. Add the IOKit framework to the project.
  3. Add the files InputController.h, InputController.m, InputDevice.h, and InputDevice.m to the project.
  4. Open the file MainMenu.nib in Interface Builder.
  5. Add a pop-up button control, static text items, and two buttons to the window to match the screenshot. Leave the text fields on the right side (USB, 1452, 516, 290, etc.) blank. The bindings are going to fill those values when you run the program.
  6. Instantiate InputController and InputDevice. Either drag the header files from Xcode to the nib file window in Interface Builder or choose Classes > Read Files in Interface Builder.

Next (Create the Controller)

Previous (Introduction)