OSX programmatically invoke sound level graphic

前端 未结 2 1648
鱼传尺愫
鱼传尺愫 2021-01-14 16:59

I have an app which can change the volume under OSX. What it lacks is the visual feedback provided when one presses the sound up/down keys. Does anyone know how to programma

相关标签:
2条回答
  • 2021-01-14 17:20

    I would implement this by simulating the physical press of the up/down volume keys, and letting the OS deal with the details. Perhaps the user has disabled the visual feedback, perhaps it changes, etc. - this is the safest way of pulling it off.

    Have a look at this: Simulating key press events in Mac OS X

    0 讨论(0)
  • 2021-01-14 17:30

    Here's a little code from George Warner and Casey Fleser that does this trick. Think carefully that this is really the way you want to do things.

    // 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);
    }
    

    Other interesting keycodes include: NX_KEYTYPE_SOUND_DOWN, NX_KEYTYPE_MUTE, NX_KEYTYPE_PLAY.

    0 讨论(0)
提交回复
热议问题