I'm trying to create a native, global, focus-independent hotkey control for incrementing/decrementing/mute-toggling system volume on Windows, specifically W10, though I expect whatever solution we find will likely work back through 7 and 8/8.1 as well.
I have found this - How can I mute/unmute my sound from PowerShell - and it works a treat, from within a PowerShell window. Ok, but I need this Type to be persistent and global then?
I then created three files, -.ps1, +.ps1, and 0.ps1, which include the entire type definition from the linked post, and then a one liner each, as follows:
[Audio]::Volume = [Audio]::Volume - .2
[Audio]::Volume = [Audio]::Volume + .2
[Audio]::Mute = [Audio]::Mute -bxor 1
Technically, all that works, but running each one pops open a PS window for 50 milliseconds or so. That's ugly, but maybe unavoidable.
Then I found this, - How to run a PowerShell script without displaying a window? - to use PSRun.exe. Turns out that in no case could I get that to successfully execute any of these scripts. I used shortcuts (.lnk) with arguments and .cmd files, and only got compilation errors.
So I tried this - https://www.faqforge.com/windows/how-to-execute-powershell-scripts-without-pop-up-window/ - using a VBS helper to hide the window. That works too, but I'd rather not use VBS if possible.
I then created a folder, %homedrive%\Users\\AppData\Roaming\Microsoft\Windows\Start Menu_hotkeys, and put one shortcut each to the .ps1 files, and assigned Alt-F6, Alt-F7, and Alt-F8 to the shortcuts.
When I run one of the shortcuts by GUI clicking, it takes about 100 milliseconds for either the .vbs or the .ps1 directly. However, when running either shortcut via hotkey, it takes more than a second!
I imagine there might be two (or more) solutions:
- Can I increase the performance of a Windows shell hotkey that runs a shortcut to a script file? I realize that call stack looks silly long for something that seems like it should be so simple of a task.
OR
- Can I create a persistent PowerShell window and run scripts into it by use of a global, focus-independent hotkey?
I'd even entertain building a TSR that does nothing but silently awaits hotkeys for volume control, but that seems less "nice" than working this out with PowerShell.
And before you ask, it's not a performance issue from this hardware, I'm running W10x64 on an i7-7700K with 16GB of RAM and a 128GB M.2. Running 40 Firefox tabs and 25 applications gets me to about 20% CPU use and 40% RAM use.
Feel free to slap me around if this question already has a solid, functional answer and my Google-Fu just didn't find it. My searches turned up things from across the last 5 years, but nothing conclusive or properly functional.