Search code examples
windowsscriptingautohotkeyhotkeyswindows-scripting

Create a script in Windows 7 that controls volume


I'm relatively new to programming and I want to make a script that controls the volume of my pc, linkt to hotkeys. What I want it to do specifically is whenever I press a key on my keyboard (i.e. ctrl + Alt + arrow up), it turns the volume to 75% and when I press another key (i.e. ctrl + Alt + arrow down), it turns it to 15%.

What type of script would I need to make, where shoult it be saved and how do I setup a hotkey to trigger it? (Also, I want this to work as soon as my pc boots up, or right thereafter). You don't need to prewrite a script for me, just point me in the right direction and I'll find my way.

This is what I've got so far:

; Volume control (turn master volume to 75% or 15%)
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

^!F12::Soundset, 75
^!F11::Soundset, 15

This works fine, but I use it on a laptop, which also has a function (fn) key to in-/decrease volume and mute it. Now, whenever I mute using the function key, I cannot set de volume to 75% with the hotkey. So I was wondering if I could override the function key (or whenever I press ctrl + fn + volume up/down it sets it to 75%, or 15%, respectivily). Also the script doesn't give any visual feedback, apart from the volume icon showing more or less volume bars (or however it's called). How would I go about doing this?

ALso, do I need those first few lines (e.g. #NoEnv, #Warn,...) or are they just for failsafe?

Any help would be much appreciated.


Solution

  • Look at the link in 2501's comment first. Try some things for yourself.

    Then look here: https://autohotkey.com/docs/commands/SoundSet.htm which will tell you about SoundSet. Then try some more things yourself.

    SoundSet

    Changes various settings of a sound device (master mute, master volume, etc.)

    SoundSet, NewSetting [, ComponentType, ControlType, DeviceNumber]

    NewSetting: Percentage number between -100 and 100 inclusive (it can be a floating point number or expression). If the number begins with a plus or minus sign, the current setting will be adjusted up or down by the indicated amount. Otherwise, the setting will be set explicitly to the level indicated by NewSetting.