我编写了一个VB脚本代码,用do循环来模拟按键。该程序在指定的时间间隔后模拟键盘上的击键事件。我需要在脚本中添加一个消息框,以便在我的工作结束时将其关闭。目前,它在后台运行无限时间,直到您注销系统。
此循环应该在后台运行,直到有人使用MsgBox中的“是/否”按钮手动结束它,或者建议以任何其他方式关闭此脚本。
以下是我写的代码:
Set Wshell=CreateObject("Wscript.shell")
Do
Wshell.SendKeys "{SCROLLLOCK}"
WScript.sleep 10000
Loop
我也尝试过使用select语句,但它似乎不起作用。
答案 0 :(得分:2)
它可让您在下一次按下按钮之前退出。 要结束脚本按需并让其他脚本在后台运行,您需要另一个解决方案,最多是一个不同的程序,它为您提供一个可以阻止它的小接口。或者是第二个专门结束第一个脚本的脚本。
Set Wshell=CreateObject("Wscript.shell")
Do
Wshell.SendKeys "{SCROLLLOCK}"
continue = MsgBox ("Do you want to press the ScrollLock again?", vbYesNo, "Question")
Select Case continue
Case vbNo
Exit Do
End Select
WScript.sleep 10000
Loop
从这篇文章中摘录:How to stop a vb script running in windows
Option Explicit
Dim WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "taskkill /f /im Cscript.exe", , True
WshShell.Run "taskkill /f /im wscript.exe", , True
还有第二个脚本可以用来杀死你的脚本。我不知道你会怎么想停止剧本?如果没有GUI,您无法在您希望的时间进行操作。要结合这些解决方案,您可以执行以下操作:
continue = MsgBox ("Do you want to press the ScrollLock again?", vbYesNo, "Question")
Select Case continue
Case vbNo
Dim WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "taskkill /f /im Cscript.exe", , True
WshShell.Run "taskkill /f /im wscript.exe", , True
End Select
然后拉出这个弹出屏幕一角的MsgBox,所以如果你不再想要它,只需按否,你就会好起来。 ^^
答案 1 :(得分:1)
您可以尝试这样的问题来提出停止脚本的问题:
Option Explicit
Dim Title,Ws
Title = "Ask a question to stop the script !"
Set Ws=CreateObject("Wscript.shell")
Do
Ws.SendKeys "{SCROLLLOCK}"
WScript.sleep 10000
Call Ask_Question()
Loop
Sub Ask_Question()
Dim Answer
Answer=MsgBox("Did you want to stop this script ?"_
& vbcr & "( Yes / No ) ?",vbQuestion+vbYesNo,Title)
If Answer=vbYes Then
Wscript.Quit(0)
Else
Exit Sub
End If
End Sub
编辑于2016年8月19日@ 12:53
只是一个一般的例子:
因为,我不知道你监控的是什么程序,所以我计划以Notepad.exe
为例
此脚本可以检查程序Notepad.exe
是否正在运行
如果不是这样,它会要求你停止剧本!
Option Explicit
Dim ProcessPath,WshShell
ProcessPath = "%Windir%\System32\Notepad.exe"
Set WshShell = CreateObject("WScript.Shell")
If AppPrevInstance() Then
MsgBox "There is an existing proceeding !" & VbCrLF &_
CommandLineLike(WScript.ScriptName),VbExclamation,"There is an existing proceeding !"
WScript.Quit
Else
Do
Call Main()
Pause(10) ' Pause 10 seconds
If CheckProcess(DblQuote(ProcessPath)) = False Then
Call Ask_Question()
End If
Loop
End If
'**************************************************************************
Function CheckProcess(ProcessPath)
Dim strComputer,objWMIService,colProcesses,Tab,ProcessName
strComputer = "."
Tab = Split(ProcessPath,"\")
ProcessName = Tab(UBound(Tab))
ProcessName = Replace(ProcessName,Chr(34),"")
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = '"& ProcessName & "'")
If colProcesses.Count = 0 Then
CheckProcess = False
Else
CheckProcess = True
End if
End Function
'**************************************************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'**************************************************************************
Sub Pause(Secs)
Wscript.Sleep(Secs * 1000)
End Sub
'**************************************************************************
Function AppPrevInstance()
With GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
With .ExecQuery("SELECT * FROM Win32_Process WHERE CommandLine LIKE " & CommandLineLike(WScript.ScriptFullName) & _
" AND CommandLine LIKE '%WScript%' OR CommandLine LIKE '%cscript%'")
AppPrevInstance = (.Count > 1)
End With
End With
End Function
'***************************************************************************
Function CommandLineLike(ProcessPath)
ProcessPath = Replace(ProcessPath, "\", "\\")
CommandLineLike = "'%" & ProcessPath & "%'"
End Function
'****************************************************************************
Sub Main()
WshShell.SendKeys "{SCROLLLOCK}"
End Sub
'****************************************************************************
Sub Ask_Question()
Dim Answer,Title
Title = "Ask a question to stop the script !"
Answer=MsgBox("Did you want to stop this script ?"_
& vbcr & "( Yes / No ) ?",vbQuestion+vbYesNo,Title)
If Answer=vbYes Then
Wscript.Quit(0)
Else
Exit Sub
End If
End Sub
'****************************************************************************