1

I'm trying to mimic holding down the volume up key for a variable number of seconds. I've tried to get a simple test going, but I think there is something I'm missing. Any help will be appreciated. My current code is based on this: https://stackoverflow.com/a/10520629/2763703.

Here is what it looks like:

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

    // Key press event
    __block UInt32 evtInfo = auxKeyCode << 16 | NX_KEYDOWN << 8;
    bzero(&event, sizeof(NXEventData));
    event.key.origCharSet = event.key.charSet = NX_ASCIISET;
    event.compound.subType = NX_SUBTYPE_AUX_CONTROL_BUTTONS;
    event.compound.misc.L[0] = evtInfo;
//    event.key.repeat = true;
    kr = IOHIDPostEvent( get_event_driver(), NX_SYSDEFINED, loc, &event, kNXEventDataVersion, 0, FALSE );
    assert( KERN_SUCCESS == kr );

    // Key release event
    dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 2);
    dispatch_after(delay, dispatch_get_main_queue(), ^(void){
        evtInfo = auxKeyCode << 16 | NX_KEYUP << 8;
        bzero(&event, sizeof(NXEventData));
        event.key.origCharSet = event.key.charSet = NX_ASCIISET;
        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 );
        assert( KERN_SUCCESS == kr );
    });
}

If I call this like HIDPostAuxKey(NX_KEYTYPE_SOUND_UP) it acts strange. It will increase the volume one block, but continue making the volume up sound for a couple more seconds. What I want is it to act just like you were holding down the volume up key on a keyboard. I thought the event.key.repeat = true; line would do the trick, but that just seems to show the "Are you sure you want to shut down your computer now" dialog. Thanks for your help.

Community
  • 1
  • 1

0 Answers0