如何使用 JScript 或 VBScript 控制 Windows 系统音量? [英] How to control Windows system volume using JScript or VBScript?

查看:19
本文介绍了如何使用 JScript 或 VBScript 控制 Windows 系统音量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过 JScript 或 VBScript 脚本控制我的 Windows 系统的音量.有什么想法吗?

I want to control the volume of my Windows system from a JScript or VBScript script. Any ideas?

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

Also, can I unmute the system volume if it is muted?

推荐答案

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

To mute or unmute the system volume, you can simulate the Mute key press using the WshShell.SendKeys method:

var oShell = new ActiveXObject("WScript.Shell");
oShell.SendKeys(Chr(&HAD));

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

As for changing the volume level from a script, there's a solution that involves some Windows automation, such as launching the System Volume applet and simulating the appropriate keyboard shortcuts in it, but I don't think it's reliable. Therefore I recommend that you use some external utility capable of changing the volume level, and call it from your script. For example, you could use the free NirCmd tool:

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");

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

NirCmd can also mute or unmute the system volume:

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

这篇关于如何使用 JScript 或 VBScript 控制 Windows 系统音量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆