Universal MediaKeys

Some time ago George Warner created a handy little set of utilities for simulating various key presses and it can still be found on his .mac file sharing page along with all sorts of other goodies. Really worth a look.

However I noticed a little wrinkle in the HIDPostAuxKey function (MediaKeys project) this morning which is listed as:

static void HIDPostAuxKey(const UInt8 auxKeyCode )
{
NXEventData		event;
kern_return_t		kr;
IOGPoint		loc = { 0, 0 };

bzero(&event, sizeof(NXEventData));

event.compound.subType = NX_SUBTYPE_AUX_CONTROL_BUTTONS;
event.compound.misc.S[0] = auxKeyCode;
event.compound.misc.C[2] = NX_KEYDOWN;

kr = IOHIDPostEvent( get_event_driver(), NX_SYSDEFINED, loc, &event, kNXEventDataVersion, 0, FALSE );
check( KERN_SUCCESS == kr );
}

This is great on a big endian machine, but really the misc data wants to be written as a long or it won’t be interpreted correctly on a little endian (Intel) machine. You probably want to do something like this:

static void HIDPostAuxKey(const UInt8 auxKeyCode )
{
NXEventData		event;
kern_return_t	kr;
IOGPoint		loc = { 0, 0 };
UInt32			evtInfo = inAuxKeyCode << 16 | NX_KEYDOWN << 8;

bzero(&event, sizeof(NXEventData));

event.compound.subType = NX_SUBTYPE_AUX_CONTROL_BUTTONS;
event.compound.misc.L[0] = evtInfo;

kr = IOHIDPostEvent( get_event_driver(), NX_SYSDEFINED, loc, &event, kNXEventDataVersion, 0, FALSE );
check( KERN_SUCCESS == kr );
}

Anyway, thought someone might find that useful.