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

Published on
8,394 Points
2 Endorsements
Last Modified:
Joe Winograd - EE Fellow & MVE
50+ years in computer industry. Everything from development to sales. CIO. Document imaging. EE MVE 2015, EE MVE 2016, EE FELLOW 2017.
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.

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")
ExitApp ; if Toggle Mute was not selected from context menu, it must be 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).

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

  • 2

Expert Comment

by:Obaid ur Rehman
Great article, Mr. Joe!
LVL 58

Author Comment

by:Joe Winograd - EE Fellow & MVE
Thank you for the compliment and article endorsement, Obaid — I really appreciate it! Regards, Joe
LVL 101

Expert Comment

by:John Hurst
Thanks for posting this, Joe.  My own ThinkPad has a mute button and I use it so much my finger is trained to it.
LVL 58

Author Comment

by:Joe Winograd - EE Fellow & MVE
You're welcome, John, and thanks to you for the feedback and the article endorsement — both very much appreciated! Regards, Joe

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Join & Write a Comment

Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month