In a question here at Experts Exchange, a member was looking for "a little app that would allow sound to be turned OFF and ON by simply clicking on an icon in the system tray". This article shows how to achieve that, as well as providing the same OFF/ON audio muting toggle via a single keystroke.
Ever visit a web page and, suddenly, your speakers start blaring?! The first thing we reach for in that case is the mute key. Many multi-media keyboards have such a key/button, but, unfortunately, the trimmed-down keyboard on many laptops do not have one. That's the case for an Experts Exchange member who posted a question looking for "a little app that would allow sound to be turned OFF and ON by simply clicking on an icon in the system tray".
Of course, most folks have the standard speaker icon in the system tray, which looks like this when unmuted:
And like this when muted:
A left-click on the speaker displays a pop-up that allows another left-click to toggle the speaker setting — looks like this when unmuted:
And like this when muted:
So, that's only two clicks — not bad, but it's one click too many when the speakers are blaring. :) Also, mousing to the system tray to do the clicking can take an agonizing amount of time under the circumstances, so a single keystroke, such as F12, may be the preferable approach. In addition, using the keyboard, rather than clicking on a small icon, is the better, sometimes necessary, method for the visually impaired.
What to do on a laptop (or even a keyboard on a desktop) that doesn't have a mute key? There are no doubt many solutions for this requirement — this article describes one. I hope that some readers jump in with others.
The solution presented here is a relatively simple script — fewer than 40 lines of code — written in the AutoHotkey language. If you are not familiar with AutoHotkey, this Experts Exchange article will get you going on it:
AutoHotkey - Getting Started
Here is the full source code for the script, which I call ToggleMute:
#Warn,UseUnsetLocal ; warn when local variable is used before it is set
#NoEnv ; do not check empty variables to see if they are environment variables
#SingleInstance force ; skip dialog box and replace old instance immediately
SetBatchLines,-1 ; run at maximum speed
Gosub,ConfigureTray ; configure the system tray with an icon and tooltip
Return ; end auto-execute section
F12:: ; define hotkey to toggle mute - this one is F12
#F11:: ; define hotkey to toggle mute - this one is Win-F11
+F10:: ; define hotkey to toggle mute - this one is Shift-F10
!^m:: ; define hotkey to toggle mute - this one is Alt-Ctrl-m
Menu,Tray,NoStandard ; do not use standard AutoHotkey context menu
Menu,Tray,Add,Toggle Mute,ContextMenu ; first context menu item toggles mute
Menu,Tray,Add,Exit,ContextMenu ; second context menu item exits
Menu,Tray,Default,Toggle Mute ; default is to toggle mute
Menu,Tray,Click,1 ; allow single-click to activate default menu item
TrayTip:="Single left-click to toggle Mute`nRight-click to Exit"
TrayIconFile:=A_WinDir . "\System32\shell32.dll" ; get tray icon from shell32.dll
TrayIconNum:="-225" ; use the music note as the tray icon (icon 225 in shell32)
Menu,Tray,Tip,%TrayTip% ; display tray tooltip
Menu,Tray,Icon,%TrayIconFile%,%TrayIconNum% ; display tray icon
SoundSet,+1,,Mute ; toggle the mute setting
If (A_ThisMenuItem="Toggle Mute")
; if "Toggle Mute" was not selected from context menu, it must be "Exit"
; make sure it was not an accident, and make No the default Yes/No button
MsgBox,4388,Exiting,Are you sure you want to exit?
I used highly descriptive label and variable names, and placed a comment on nearly every line of code, to act as a form of documentation for the script. I hope that everything in it is clear, but if readers have any questions, please post them here and I'll be happy to try to answer them.
As with any AutoHotkey script, you may run ToggleMute from the source code after installing AutoHotkey, or you may compile it into a stand-alone executable (a .EXE file) that can run on any Windows computer, i.e., AutoHotkey does not have to be on the computer to run the compiled ToggleMute EXE file.
ToggleMute places a music note icon in the system tray that looks like this:
A single left-click on that icon toggles the mute setting — from ON to OFF or from OFF to ON. Hovering on the icon shows this brief tooltip help:
ToggleMute also defines a hotkey that toggles the mute setting. For example, in the source code shown above, F12 (by itself, with no modifier keys) is defined as a key that toggles the mute setting. I also put other hotkeys in the script so you can get a sense of how to specify the modifiers for hotkeys, namely, the octothorpe (# — aka hash, number sign, and pound sign) for the Windows logo key, the plus sign (+) for the Shift key, the exclamation mark (!) for the Alt key, and the circumflex (^ — aka caret and hat) for the Ctrl key. For example, with the code shown above, Alt-Ctrl-m toggles the mute setting.
A right-click on the music note icon displays this simple context menu:
Clicking Exit terminates the ToggleMute app, thereby removing the ToggleMute icon from the system tray and removing the ToggleMute hotkey assignment(s).
Article update 16-Aug-2018: A user of ToggleMute requested the addition of a confirmation dialog when Exit is clicked from the context menu so that an accidental termination does not occur. I updated the code above to have this feature. I made No the default Yes/No button so that an accidental Enter key would not cause exiting.
If you find this article to be helpful, please click the thumbs-up icon below. This lets me know what is valuable for EE members and provides direction for future articles. Thanks very much! Regards, Joe