1
我剛剛使用以下代碼(來源:https://stackoverflow.com/a/10520629/1213411)來播放/暫停音樂。現在我想跳過當前曲目並跳到下一曲。所以我用代碼HIDPostAuxKey(NX_KEYTYPE_NEXT);
來做到這一點。但這似乎並不奏效? 有沒有人有一個想法,爲什麼這不起作用?使用媒體鍵跳過曲目
在此先感謝!
// Save as sound_up.m
// Compile: gcc -o sound_up sound_up.m -framework IOKit -framework Cocoa
#import <Cocoa/Cocoa.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 HIDPostAuxKey(const UInt8 auxKeyCode)
{
NXEventData event;
kern_return_t kr;
IOGPoint loc = { 0, 0 };
// Key press event
UInt32 evtInfo = auxKeyCode << 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);
// Key release event
evtInfo = auxKeyCode << 16 | NX_KEYUP << 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);
}
int main(int argc, char *argv[]) {
HIDPostAuxKey(NX_KEYTYPE_SOUND_UP);
}