How to create an on/off toggle to mute the system audio/sound with a single mouse click or single keystroke

Joe WinogradDeveloper
CERTIFIED EXPERT
50+ years in computers
EE FELLOW 2017 — first ever recipient of Fellow award
MVE 2015,2016,2018
CERTIFIED GOLD EXPERT
DISTINGUISHED EXPERT
Published:
Updated:
Edited by: Andrew Leniart
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
Gosub,ToggleMute
Return

ConfigureTray:
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
Return

ToggleMute:
SoundSet,+1,,Mute ; toggle the mute setting
Return

ContextMenu:
If (A_ThisMenuItem="Toggle Mute")
{
  Gosub,ToggleMute
  Return
}
; 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?
IfMsgBox,Yes
  ExitApp
Else
  Return


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.


Article update 3-Oct-2019: An Experts Exchange member noted that the screenshots in this article are from a Windows 7 system and was wondering if ToggleMute works on Windows 10 — it does. It also works on XP, Vista, and W8/8.1 (and possibly earlier versions, but not tested on Windows prior to XP). Here are the screenshots from a W10 system:






Article update 19-Aug-2023: Tested on Windows 11 — works perfectly!


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

6
9,136 Views
Joe WinogradDeveloper
CERTIFIED EXPERT
50+ years in computers
EE FELLOW 2017 — first ever recipient of Fellow award
MVE 2015,2016,2018
CERTIFIED GOLD EXPERT
DISTINGUISHED EXPERT

Comments (4)

Obaid ur RehmanTechnical and Operations Manager

Commented:
Great article, Mr. Joe!
Joe WinogradDeveloper
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2018

Author

Commented:
Thank you for the compliment and article endorsement, Obaid — I really appreciate it! Regards, Joe
JohnBusiness Consultant (Owner)
Most Valuable Expert 2012
Expert of the Year 2018

Commented:
Thanks for posting this, Joe.  My own ThinkPad has a mute button and I use it so much my finger is trained to it.
Joe WinogradDeveloper
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2018

Author

Commented:
You're welcome, John, and thanks to you for the feedback and the article endorsement — both very much appreciated! Regards, Joe

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.