如何使用JScript或VBScript控制Windows系统卷?

Sib*_*Lin 9 javascript windows vbscript volume mute

我想从JScript或VBScript脚本控制我的Windows系统的卷.有任何想法吗?

此外,如果系统音量静音,我可以取消静音吗?

Hel*_*len 9

要使系统音量静音或取消静音,您可以使用以下WshShell.SendKeys方法模拟静音键按下:

var oShell = new ActiveXObject("WScript.Shell");
oShell.SendKeys(Chr(&HAD));
Run Code Online (Sandbox Code Playgroud)

至于从脚本更改音量级别,有一个涉及一些Windows自动化的解决方案,例如启动System Volume applet并模拟其中的相应键盘快捷键,但我认为它不可靠.因此,我建议您使用一些能够更改音量级别的外部实用程序,并从脚本中调用它.例如,您可以使用免费的NirCmd工具:

var oShell = new ActiveXObject("WScript.Shell");

// Increase the system volume by 20000 units (out of 65535)
oShell.Run("nircmd.exe changesysvolume 20000");

// Decrease the system volume by 5000 units
oShell.Run("nircmd.exe changesysvolume -5000");
Run Code Online (Sandbox Code Playgroud)

NirCmd还可以将系统音量静音或取消静音:

var oShell = new ActiveXObject("WScript.Shell");
oShell.Run("nircmd.exe mutesysvolume 0");  // unmute
oShell.Run("nircmd.exe mutesysvolume 1");  // mute
oShell.Run("nircmd.exe mutesysvolume 2");  // switch between mute and unmute
Run Code Online (Sandbox Code Playgroud)

  • 即使对于Windows 7(32位),这也可以完美运行!然而,我有一个修正,因为"mutesysvolume 0"是静音而"mutesysvolume 1"是静音. (2认同)
  • nircmd 很棒,甚至可以控制应用程序音量和单独的混音器控件 (2认同)

小智 8

我可以看到在Windows上操作系统卷级别而无需安装其他软件的最佳方法是使用以下方法之一使用VBScript:

切换静音:(在之前的回答中已经提到)

Set WshShell = CreateObject("WScript.Shell")
WshShell.SendKeys(chr(&hAD))
Run Code Online (Sandbox Code Playgroud)

提高音量:

Set WshShell = CreateObject("WScript.Shell")
WshShell.SendKeys(chr(&hAF))
Run Code Online (Sandbox Code Playgroud)

降低音量:

Set WshShell = CreateObject("WScript.Shell")
WshShell.SendKeys(chr(&hAE))
Run Code Online (Sandbox Code Playgroud)

这适用于大多数现代Windows机器.我已经在Windows 7和8.1上测试了它,即使在LC中使用"do as"运行它也能正常工作,因此可以将这些脚本嵌入到可执行文件中并运行它们而无需将它们另存为单个文件.