#import <IOKit/IOKitLib.h>
#import <IOKit/hidsystem/IOHIDLib.h>
#import <IOKit/hidsystem/ev_keymap.h>
static io_connect_t get_event_driver(void)
{
static mach_port_t sEventDrvrRef = 0;
mach_port_t masterPort, service, iter;
kern_return_t kr;
if (!sEventDrvrRef) {
// Get master device port
kr = IOMasterPort( bootstrap_port, &masterPort );
check( KERN_SUCCESS == kr);
kr = IOServiceGetMatchingServices( masterPort, IOServiceMatching(kIOHIDSystemClass), &iter );
check( KERN_SUCCESS == kr);
service = IOIteratorNext( iter );
check( service );
kr = IOServiceOpen( service, mach_task_self(), kIOHIDParamConnectType, &sEventDrvrRef );
check( KERN_SUCCESS == kr );
IOObjectRelease( service );
IOObjectRelease( iter );
}
return sEventDrvrRef;
}
static void HIDPostAuxKeyWithState(UInt8 auxKeyCode, UInt8 keyState)
{
NXEventData event;
kern_return_t kr;
IOGPoint loc = { 0, 0 };
bzero(&event, sizeof(NXEventData));
event.compound.subType = NX_SUBTYPE_AUX_CONTROL_BUTTONS;
if (OSLittleEndian == OSHostByteOrder()) {
UInt32 evtInfo = auxKeyCode << 16 | keyState << 8;
event.compound.misc.L[0] = evtInfo;
}else {
event.compound.misc.S[0] = auxKeyCode;
event.compound.misc.C[2] = keyState;
}
kr = IOHIDPostEvent( get_event_driver(), NX_SYSDEFINED, loc, &event, kNXEventDataVersion, 0, FALSE );
check( KERN_SUCCESS == kr );
}
static void HIDPostAuxKey(UInt8 auxKeyCode)
{
HIDPostAuxKeyWithState(auxKeyCode, NX_KEYDOWN);
HIDPostAuxKeyWithState(auxKeyCode, NX_KEYUP);
}
使用方法:
HIDPostAuxKey(NX_KEYTYPE_SOUND_UP);