Automatic Scroll Wheel Speaker Volume Control When Hovering Over the Taskbar

Put simple Tips and Tricks that are not entire Tutorials in this forum
User avatar
jackdunning
Posts: 126
Joined: 01 Aug 2016, 18:17
Contact:

Automatic Scroll Wheel Speaker Volume Control When Hovering Over the Taskbar

Post by jackdunning » 21 Sep 2019, 14:39

This short tip comes directly from the AutoHotkey documentation for the #If directive. I republish it here for those who haven't had a chance to read all the online documentation. Plus, a while back I added it to my main AutoHotkey script and now use my mouse scroll wheel hovering over the Windows Taskbar as my primary method for controlling speaker volume.

Image

A number of other scripts of this type have appeared in the forum, but I have found none quite so simple and easy-to-use.

The #IfWinActive directive restricts Hotkeys to particular active program windows. However, you'll find many times when expressions prove useful for determining whether or not to activate a Hotkey. As in this example, the Hotkey works only when the mouse cursor hovers over the Taskbar. The following script does this by implementing an #If expression directive which checks the mouse cursor location:

Code: Select all

#If MouseIsOver("ahk_class Shell_TrayWnd")
   WheelUp::Send {Volume_Up}
   WheelDown::Send {Volume_Down}
#If

MouseIsOver(WinTitle)
{  MouseGetPos,,, Win
   Return WinExist(WinTitle . " ahk_id " . Win)
}
I discuss this code (and other examples of mouse cursor location-sensitive AutoHotkey applications) in Chapter Six of the book AutoHotkey Hotkeys: Tips, Tricks, Techniques, and Best Practices.
AviMP
Posts: 1
Joined: 31 May 2020, 10:35

Re: Automatic Scroll Wheel Speaker Volume Control When Hovering Over the Taskbar

Post by AviMP » 31 May 2020, 10:43

Thank you! I've been trying lots of programs to do Volume Control with mouse wheel. None of these programs is perfect. Some does not work well, some requires updates after each major Windows 10 update, some displays ugly popups. Your solution is very simple and effective.
hege
Posts: 1
Joined: 23 Sep 2020, 03:15

Re: Automatic Scroll Wheel Speaker Volume Control When Hovering Over the Taskbar

Post by hege » 23 Sep 2020, 03:22

Had to register to thank for this. Volume2 was the only software I found that had this feature, but it created other problems with my current audio card.

Here is my modification which works on bottom of the screen (40px = approx taskbar height), even in full screen programs, for example when watching videos.

Code: Select all

CoordMode, Mouse, Screen

#If MouseIsOverBottom()
   WheelUp::Send {Volume_Up 2}
   WheelDown::Send {Volume_Down 2}
#If

MouseIsOverBottom()
{  MouseGetPos,,y
   Return y > A_ScreenHeight - 40
}
Here's adapted example of non-flickering OSD that uses SoundSet and unmutes on volume change. It also has "semi-logarithmic" method to change volume by +-2 when under 20% and +-5 if over.

Code: Select all

CoordMode, Mouse, Screen

BarX := ((A_ScreenWidth / 20) * 19) - 260
BarY := (A_ScreenHeight / 20) + 52
Volume_ProgressbarOpts = CW000000 CTFFFFFF CBAAAAAA x%BarX% y%BarY% w260 h52 B1 FS8 WM700 WS700 FM8 ZH12 ZY3 C11
Progress Hide %Volume_ProgressbarOpts%,,Volume,,Tahoma

#If MouseIsOverBottom()
   WheelUp::
      ChangeVolume("+")
      gosub, Volume_Show_OSD
      return
   WheelDown::
      ChangeVolume("-")
      gosub, Volume_Show_OSD
      return
#If

ChangeVolume(x)
{  SoundGet, vol, Master, Volume
   if (x = "+")
     nd := Round(vol) < 20 ? 2 : 5
   else
     nd := Round(vol) <= 20 ? 2 : 5
   nv = %x%%nd%
   SoundSet, nv, Master, Volume
   SoundSet, 0, Master, Mute
}

MouseIsOverBottom()
{  MouseGetPos,,y
   Return y > A_ScreenHeight - 40
}

Volume_Show_OSD:
  SoundGet, Volume, Master, Volume
  Progress % Volume := Round(Volume), %Volume% `%
  Progress Show
  SetTimer, Remove_Show_OSD, 500
return

Remove_Show_OSD:
  SetTimer, Remove_Show_OSD, Off
  Progress Hide %Volume_ProgressbarOpts%,,Volume,,Tahoma
return
Post Reply

Return to “Tips and Tricks (v1)”