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