How to stop a loop when pressing “OK” on a message

2020-05-03 03:30发布

问题:

I need some help. I am a newbie in VBS and I want to write a bot for a video game I play to type stuff.

set sellAllTyping = wscript.CreateObject("WScript.Shell")

x= MsgBox ("To stop the bot, click OK. /// Coded by Vncz with a little help from StackOverflow! :^) RIP Yams ;-;",vbOK+vbInformation,"sell all Bot")

if x=vbOK then 
' *** I don't know what to write here. ***

sellAllTyping.sendkeys "t"
wscript.sleep 1000
sellAllTyping.sendkeys "/sell all"
wscript.sleep 1000
sellAllTyping.sendkeys "{ENTER}"
wscript.sleep 5000
loop

I want to have the loop on the very bottom stop if I press OK on the message box I summoned. In place of the comment, what code should I write, if I'm even doing it right? Thanks!

回答1:

Unfortuately, msgbox freeze the execution.

In others words, you cannot loop between the moment the message apear and the moment you click on Ok or Cancel­.

The only way to achieve this is to make a HTA file like this:

<SCRIPT LANGUAGE="VBScript" src="test.vbs"> </SCRIPT>
<input type='button' value='Start' onclick='startLoop()'>
<input type='button' value='Stop' onclick='stopLoop()'>

then, on the linked vbs file (for this example, it's test.vbs), you must write something like this:

set sellAllTyping = CreateObject("Wscript.shell")
loopState = true

sub startLoop()
    do while loopState = true
        msgbox "yeah"
    loop
end sub

sub stopLoop()
    loopState = false
end sub

The only problem now is that the app is freezing because of the loop (if I replace msgbox with sendkeys) or is always hide behind the msgbox.

this code work well (with a sleep function):

set sellAllTyping = CreateObject("Wscript.shell")
loopState = true

sub startLoop()
    do while loopState = true
        msgbox "yeah"
        sleep(2)
    loop
end sub

sub stopLoop()
    loopState = false
end sub

Sub sleep (Timesec)
  sellAllTyping.Run "Timeout /T " & Timesec & " /nobreak" ,0 ,true
End Sub

Make sure you name the hta file "something.hta" and NOT "something.html"



回答2:

I don't know which looping keyword you used. Use any of the below keyword after

if x=vbOK then "Exit For/Exit Do/Exit Function" might help you



标签: vbscript