如何创建一个开/关切换以通过单击一次鼠标或一次击键将系统音频/声音静音

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".

曾经访问过网页,突然间,您的扬声器开始大吼大叫?! 在这种情况下,我们要做的第一件事是静音键。 许多多媒体键盘都具有这样的键/按钮,但是不幸的是,许多笔记本电脑上的精简键盘却没有。 对于Experts Exchange成员来说就是这种情况,他发布了一个问题,寻找“一个只需单击系统任务栏中的图标即可打开和关闭声音的小应用程序”。

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.

因此,这只有两次点击-不错,但是当扬声器发出刺耳的声音时,一次点击就太多了。 :)另外,在这种情况下,将鼠标悬停在系统托盘上可能会花费大量的时间,因此,单个击键(例如F12)可能是更可取的方法。 此外,对于有视觉障碍的人,使用键盘而不是单击小图标是更好的方法,有时是必要的方法。

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:

这里介绍的解决方案是一个相对简单的脚本-少于40行代码-用AutoHotkey语言编写。 如果您不熟悉AutoHotkey,请参阅此Expert Exchange文章:

AutoHotkey - Getting Started AutoHotkey-入门

Here is the full source code for the script, which I call ToggleMute:

这是脚本的完整源代码,我称之为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.

与任何AutoHotkey脚本一样,您可以在安装AutoHotkey之后从源代码运行ToggleMute,或者可以将其编译为可以在任何Windows计算机上运行的独立可执行文件(.EXE文件),即AutoHotkey不必在计算机上运行已编译的ToggleMute EXE文件。

ToggleMute places a music note icon in the system tray that looks like this:

ToggleMute在系统托盘中放置一个音符图标,如下所示:

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:

只需在该图标上单击鼠标左键即可将静音设置从ON切换到OFF或从OFF切换到ON。 将鼠标悬停在图标上会显示以下简短的工具提示帮助:

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.

ToggleMute还定义了一个热键,用于切换静音设置。 例如,在上面显示的源代码中,F12(本身没有修饰键)被定义为切换静音设置的键。 我还将其他热键放在脚本中,以便您可以了解如何为热键指定修饰符,即Windows徽标键的octothorpe( -aka哈希,数字符号和井号),加号( Shift键为+ ,Alt键为感叹号( ),Ctrl键为抑扬符( ^ — aka插入符号和帽子)。 例如,使用上面显示的代码, Alt + Ctrl + m切换静音设置。

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).

单击“ 退出”将终止ToggleMute应用程序,从而从系统托盘中删除ToggleMute图标,并删除ToggleMute热键分配。

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.

文章更新2018年8月16日: ToggleMute的用户请求从上下文菜单中单击`` 退出''时添加一个确认对话框,以免意外终止。 我更新了上面的代码以具有此功能。 我将“ ”设置为默认的“是/否”按钮,以便意外的Enter键不会导致退出。

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:

文章更新2019年10月3日:一位Experts Exchange成员指出,本文中的屏幕截图来自Windows 7系统,并想知道ToggleMute是否可在Windows 10上使用。 它也可以在XP,Vista和W8 / 8.1(可能还有更早的版本,但未在XP之前的Windows上进行过测试)上运行。 以下是W10系统的屏幕截图:

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

如果您发现本文有帮助,请单击下面的大拇指图标。 这使我知道什么对EE成员有价值,并为以后的文章提供了指导。 非常感谢! 问候乔

翻译自: https://www.experts-exchange.com/articles/31560/How-to-create-an-on-off-toggle-to-mute-the-system-audio-sound-with-a-single-mouse-click-or-single-keystroke.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值