AutoHotkey Community

It is currently May 27th, 2012, 8:18 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: August 15th, 2010, 12:48 am 
Offline

Joined: August 15th, 2010, 12:20 am
Posts: 2
Hello, world!

The first and the most important thing that I want to say is that the method presented here does not use Windows API and therefore cannot be the absolute solution. This code was written by me to control the sound level of specific applications using the standard functions of Autohotkey. Within six months this code works well, so not finding a more suitable solution on the internet I decided to publish it here. I will be glad to answer your questions and listen to constructive criticism to improve the quality of code.

This code does the following:

When you press the F1 button on the keyboard, the volume level of the selected (active or not active) application decreases. When you press the F2 button on the keyboard, the volume level of the selected (active or not active) application increases. During the volume changes the current volume level can be seen in Volume Mixer. As an example, I used the Windows Media Player application.

Code revision: 1.01 (25.10.2010)

Code:
SetTitleMatchMode, 3
SndVolWasStarted = 0

;Turn off SndVol after 1 second
Loop {
Sleep, 10
If SndVolWasStarted = 1
   {
   GetKeyState, StateF1, F1
   GetKeyState, StateF2, F2
   If (StateF1 = "D" or StateF2 = "D")
      SndVolStartTime = %A_Now%
      Else {
      If ((A_Now - SndVolStartTime > 1) and WinExist("ahk_class #32770"))
         WinClose, ahk_class #32770
      }
   IfWinNotExist, ahk_class #32770
      SndVolWasStarted = 0
   }
}

;Hotkey to decrease volume
F1::
IfWinExist, Windows Media Player
   {
   IfWinNotExist, ahk_class #32770
      {
      Run, "%A_WinDir%\System32\SndVol.exe" -r 88888888
      WinWait, ahk_class #32770
      SndVolWasStarted = 1
      }
   ToolbarWindowNumber = 322
   msctls_trackbarNumber = 321
   Loop {
   ControlGetText, ControlName, ToolbarWindow%ToolbarWindowNumber%, ahk_class #32770
   If ControlName = Mute for Windows Media Player
      {
      ControlSend, msctls_trackbar%msctls_trackbarNumber%, {Down}, ahk_class #32770 ; Use {Down 2} to change sound level faster
      Break
      } Else {
      If ToolbarWindowNumber < 328
         {
         ToolbarWindowNumber := ToolbarWindowNumber + 2
         msctls_trackbarNumber := msctls_trackbarNumber + 1
         } Else {
         If ToolbarWindowNumber = 328
            {
            ToolbarWindowNumber = 3210
            msctls_trackbarNumber := msctls_trackbarNumber + 1
            } Else {
            If ToolbarWindowNumber < 3242
               {
               ToolbarWindowNumber := ToolbarWindowNumber + 2
               msctls_trackbarNumber := msctls_trackbarNumber + 1
               } Else {
               MsgBox, 16, AutoHotkey, ERROR: Application's volume control was not found!`nThis could occur if the Volume Mixer has more than 20 opened applications
               Break
               }
            }
         }
      }
   }
}
Return

;Hotkey to increase volume
F2::
IfWinExist, Windows Media Player
   {
   IfWinNotExist, ahk_class #32770
      {
      Run, "%A_WinDir%\System32\SndVol.exe" -r 88888888
      WinWait, ahk_class #32770
      SndVolWasStarted = 1
      }
   ToolbarWindowNumber = 322
   msctls_trackbarNumber = 321
   Loop {
   ControlGetText, ControlName, ToolbarWindow%ToolbarWindowNumber%, ahk_class #32770
   If ControlName = Mute for Windows Media Player
      {
      ControlSend, msctls_trackbar%msctls_trackbarNumber%, {Up}, ahk_class #32770 ; Use {Up 2} to change sound level faster
      Break
      } Else {
      If ToolbarWindowNumber < 328
         {
         ToolbarWindowNumber := ToolbarWindowNumber + 2
         msctls_trackbarNumber := msctls_trackbarNumber + 1
         } Else {
         If ToolbarWindowNumber = 328
            {
            ToolbarWindowNumber = 3210
            msctls_trackbarNumber := msctls_trackbarNumber + 1
            } Else {
            If ToolbarWindowNumber < 3242
               {
               ToolbarWindowNumber := ToolbarWindowNumber + 2
               msctls_trackbarNumber := msctls_trackbarNumber + 1
               } Else {
               MsgBox, 16, AutoHotkey, ERROR: Application's volume control was not found!`nThis could occur if the Volume Mixer has more than 20 opened applications
               Break
               }
            }
         }
      }
   }
}
Return

Compatible with AutoHotkey 1.0.48.05 and Windows 7


Last edited by John T on November 14th, 2010, 2:30 am, edited 5 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject: good job
PostPosted: August 15th, 2010, 1:36 pm 
Offline

Joined: August 12th, 2010, 7:05 am
Posts: 13
re!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 25th, 2010, 4:11 pm 
Offline

Joined: August 15th, 2010, 12:20 am
Posts: 2
Updated code to revision 1.01 - fixed bug with 100% volume change


Report this post
Top
 Profile  
Reply with quote  
PostPosted: April 23rd, 2011, 12:00 am 
Offline

Joined: April 17th, 2011, 2:15 am
Posts: 12
Could you explain what is going on in the main vol up/down lines?
Specifically:
Code:
   ToolbarWindowNumber = 322
   msctls_trackbarNumber = 321
   Loop {
   ControlGetText, ControlName, ToolbarWindow%ToolbarWindowNumber%, ahk_class #32770
   If ControlName = Mute for Windows Media Player
      {
      ControlSend, msctls_trackbar%msctls_trackbarNumber%, {Down}, ahk_class #32770 ; Use {Down 2} to change sound level faster
      Break
      } Else {
      If ToolbarWindowNumber < 328
         {
         ToolbarWindowNumber := ToolbarWindowNumber + 2
         msctls_trackbarNumber := msctls_trackbarNumber + 1
         } Else {
         If ToolbarWindowNumber = 328
            {
            ToolbarWindowNumber = 3210
            msctls_trackbarNumber := msctls_trackbarNumber + 1
            } Else {
            If ToolbarWindowNumber < 3242
               {
               ToolbarWindowNumber := ToolbarWindowNumber + 2
               msctls_trackbarNumber := msctls_trackbarNumber + 1
               } Else {
               MsgBox, 16, AutoHotkey, ERROR: Application's volume control was not found!`nThis could occur if the Volume Mixer has more than 20 opened applications
               Break
               }
            }
         }

I am thinking of editing this so it can handle more than WMP (Firefox for example, or better whatever window has focus), and mute things too.
I got that ahk_class #32770 is the SndVol.exe window, but I have never used ControlGetText/ControlSend lines.
How do you figure out what portion of the SndVol window is the correct WMP slider?
And I don't understand where the ToolbarWindowNumber nmbers 322, 328, 3210, etc. come from.

Thanks for the help


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 22nd, 2012, 8:40 am 
Offline

Joined: March 4th, 2011, 2:04 am
Posts: 7
could someone optimize so that would work with any application without necessary stuff? also how do i make it jump not by 1% but each 10%?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 26th, 2012, 12:42 pm 
Offline

Joined: January 8th, 2012, 9:45 pm
Posts: 4
So there is a new Audio WinAPI Vista-Core-Audio-API

Here's a tutorial on how to use them. With some help I think we can make this work for AHK too.

http://www.codeproject.com/Articles/185 ... me-Control


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Aravind, sks, Stigg and 12 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group