租号玩代理申请
A-A+

VBS新手学习代码合集解析 使用方法 实例解析 新手教程

2022年12月11日 电脑网络 暂无评论 阅读 319 次

下面举例一些常见的VPS代码使用方法:

激活窗口

Dim WshShell
Set WshShell = CreateObject("WScript.Shell")
WshShell.AppActivate "窗口标题"

模拟输入

Dim WshShell
Set WshShell = CreateObject("WScript.Shell")
WshShell.SendKeys "按键"

参考说明:

1.基本键

一般来说,要发送的按键都可以直接用该按键字符本身来表示

发送字母 X,用 WshShell.SendKeys "X"

也可直接发送多个按键指令,只需要将按键字符按顺序排列在一起即可

发送按键 Test,用 WshShell.SendKeys "Test"

2.特殊功能键
对于需要与 Shift、Ctrl、Alt 三个控制键组合的按键,SendKeys 使用特殊字符来表示:

注意:这样使用时,不用 大括号 括起这些特殊字符.

由于 +、^ 这些字符用来表示特殊的控制按键,所以如果要表示这些 字符 的按键,要用 大括号 括住这些字符

发送加号 +,用 WshShell.SendKeys "{+}"

对于一些不会生成字符的控制功能按键,也同样需要使用大括号括起来按键的名称
如果发送是基本字符用 “” 括起来

发送 回车键,用 WshShell.SendKeys "{ENTER}"
发送 向下的方向键,用 WshShell.SendKeys "{DOWN}"

3.组合键

发送 Ctrl + Shift + ESC,用 WshShell.SendKeys "^+{ESC}"

4.发送多个重复的单字母按键
不必重复输入该字母,SendKeys 允许使用简化格式进行描述
使用格式:{按键 数字}

发送 10 个字母 X,用 WshShell.SendKeys "{X 10}"

5.列表


循环

do
...
loop

语音合成 (TTS)

CreateObject("SAPI.SpVoice").Speak "内容"

暂停

WScript.Sleep 毫秒

范例 (Samples)
模拟输入
键盘灯闪烁
倒计时 (时间到后闪烁 NumLock 5 次,间隔 0.2s)

Dim WshShell
Set WshShell = CreateObject("WScript.Shell")
m = InputBox("分")
s = InputBox("秒")
WScript.sleep m*6000+s*1000
WshShell.SendKeys "{NUMLOCK}"
WScript.sleep 200
WshShell.SendKeys "{NUMLOCK}"
WScript.sleep 200
WshShell.SendKeys "{NUMLOCK}"
WScript.sleep 200
WshShell.SendKeys "{NUMLOCK}"
WScript.sleep 200
WshShell.SendKeys "{NUMLOCK}"
WScript.sleep 200
WshShell.SendKeys "{NUMLOCK}"
WScript.sleep 200
WshShell.SendKeys "{NUMLOCK}"
WScript.sleep 200
WshShell.SendKeys "{NUMLOCK}"
WScript.sleep 200
WshShell.SendKeys "{NUMLOCK}"
WScript.sleep 200
WshShell.SendKeys "{NUMLOCK}"

循环闪烁 (NumLock,间隔 1s)

Dim WshShell
Set WshShell = CreateObject("WScript.Shell")
do
WshShell.SendKeys "{NUMLOCK}"
WScript.Sleep 1000
loop

循环闪烁 (NumLock,自定义间隔)

Dim WshShell
Set WshShell = CreateObject("WScript.Shell")
t = InputBox ("请输入闪烁间隔(毫秒)")
do
WshShell.SendKeys "{NUMLOCK}"
WScript.Sleep t
loop

循环闪烁 (3 灯轮换,间隔 0.5s)

Dim WshShell
Set WshShell = CreateObject("WScript.Shell")
WshShell.SendKeys "{NUMLOCK}"
WScript.Sleep 500
WshShell.SendKeys "{NUMLOCK}"
WScript.Sleep 500
do
WshShell.SendKeys "{NUMLOCK}"
WshShell.SendKeys "{CAPSLOCK}"
WScript.Sleep 500
WshShell.SendKeys "{CAPSLOCK}"
WshShell.SendKeys "{SCROLLLOCK}"
WScript.Sleep 500
WshShell.SendKeys "{SCROLLLOCK}"
WshShell.SendKeys "{NUMLOCK}"
WScript.Sleep 500
loop

提示框
简易,有输入框

Dim i,msg
msg = "请输入内容:"
i = InputBox(msg)
Msgbox i

音乐播放器
简易,有输入框

Set Player = CreateObject("WMPlayer.OCX")
song = InputBox ("请输入歌曲路径" & vbCrLf & "" ,"简易音乐播放器  [等待中]")
if song <>"" then
do
Player.url = song
song = InputBox ( song & vbCrLf & "" ,"简易音乐播放器  [正在播放]",song) 
if song = "" then exit do
loop
end if
Set Player = nothing

语音合成
有输入框

Set s = CreateObject("SAPI.SpVoice")
i = InputBox ("请输入内容" & vbCrLf & "" ,"语音合成  [等待中]")
s.speak i

原文:https://www.jianshu.com/p/d82426316c85?ivk_sa=1024320u

京东空调大促销