windowswindows-10autohotkeyvolume

How to trigger the Windows volume display?


When the standard keyboard key to increase the volume is hit on windows, a small window appears in the upper left displaying the volume and possibly information about playing media. I am looking for a way to trigger the window without changing the volume status, preferably in an easy to integrate way with Autohotkey.


Solution

  • The most robust way to trigger the media display with minimal impact to playing audio is to save the mute status and volume level, Send a volume button keypress, then restore the mute and volume levels. To prevent a blip of noise when muted set the volume to 0 then press Volume_Down specifically. To avoid unintentional muting at low volumes, use Volume_Up otherwise.

    Volume keys are preferred over sending Volume_Mute twice since that approach causes a noticeable gap in the audio.

    All code is in AHK v2.

    ; Trigger the volume/media display without affecting playing audio by hitting
    ;   `Volume_Up` then quickly resetting the volume. When muted set the volume
    ;   to 0 and send `Volume_Down` instead to avoid a blip of noise.
    ^PgUp::{
        volume:=SoundGetVolume()
        muted:=SoundGetMute()
    
        ; Trigger the display, being careful to not let sound leak out
        if (muted or volume == 0) {
            SoundSetVolume(0)
            Send "{Volume_Down}"
        } else {
            Send "{Volume_Up}"
        }
    
        ; Reset to the original volume and mute status
        SoundSetMute(muted)
        SoundSetVolume(volume)
    }
    

    Related functions to make volume button presses increment by 1% instead of 2% while still triggering the volume/media display:

    ; Prefix with `$` to prevent self triggering from the `Volume_Up` sent inside
    $Volume_Up::{
        ; `SoundGetVolume` returns a float which is rounded to prevent error accumulating
        ; Remove the `Round` if planning to increment by a non-integer value instead of 1
        volume:=Round(SoundGetVolume())
    
        ; Send a `Volume_Up` keypress to trigger the media display and unmute (if muted)
        Send "{Volume_Up}"
    
        ; Indiscriminately set the volume manually to volume+1
        SoundSetVolume(volume+1)
    }
    
    ; Much the same as above, yet when sending `Volume_Down` at volumes <=3 the volume
    ;   rounds down to 0, triggering mute. To avoid a gap in the audio, can either 
    ;   set the volume to 4 when (1<volume<=3) before sending `Volume_Down` or
    ;   manually undo the muting afterwards (if volume > 1). This causes either a
    ;   brief volume jump or mute blip, respectively.
    $Volume_Down::{
        volume:=Round(SoundGetVolume())
    
        ; Bumping the volume before sending `Volume_Down` to prevent a mute blip
        if(1 < volume and volume <= 3){
            SoundSetVolume(4)
        }
    
        Send "{Volume_Down}"
        SoundSetVolume(volume-1)
    
        ; (The alternative approach, delete the volume bump code above when using)
        ;   ; Undo unintentionally triggered mute when initial volume was <= 3
        ;   if(volume > 1) {
        ;      SoundSetMute(0)
        ;   }
    }
    

    All of the above code, without the descriptive comments:

    ^PgUp::{
        volume:=SoundGetVolume()
        muted:=SoundGetMute()
        if (muted or volume == 0) {
            SoundSetVolume(0)
            Send "{Volume_Down}"
        } else {
            Send "{Volume_Up}"
        }
        SoundSetMute(muted)
        SoundSetVolume(volume)
    }
    
    $Volume_Up::{
        volume:=Round(SoundGetVolume())
        Send "{Volume_Up}"
        SoundSetVolume(volume+1)
    }
    
    $Volume_Down::{
        volume:=Round(SoundGetVolume())
        if(1 < volume and volume <= 3){
            SoundSetVolume(4)
        }
        Send "{Volume_Down}"
        SoundSetVolume(volume-1)
    }