需要在 VBscript 中感应击键以结束循环 [英] Need to sense a keystroke in VBscript to end a loop

查看:20
本文介绍了需要在 VBscript 中感应击键以结束循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 VBscript 真的很陌生;就像我半小时前刚刚开始一样,但我只需要它来编写一个超级简单的程序.它运行一个 Do until 循环,我希望它在我按下空格键时停止.我已经完成了其他所有工作,我只需要知道是否/如何检测击键.提前致谢!

I am really new to VBscript; like I litteraly just started about a half hour ago, but I just need it for a super simple program. It runs a Do Until loop, and I want it to stop when I press the spacebar. I have everything else done, I just need to know if/how to detect the keystroke. Thanks in advance!

推荐答案

这是一个操作性的、不可取的(建议您)选项.但它每次检查按键时都会生成一行写入控制台(跳转到下一行).

This is a operative, non advisable (you are advised), option. But it generates a line write to console (jump to next line) every time it checks for the key press.

Function GetParentProcessId()
    Dim processesList, process
    Set processesList = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
    Set processesList = processesList.ExecQuery("SELECT * FROM Win32_Process WHERE (Name = 'cscript.exe') AND Commandline LIKE '%"+WScript.ScriptName+"%'" )
    For Each process in processesList
        GetParentProcessId = process.ParentProcessId
    Next 
End Function 

Dim parentProcessId
    parentProcessId = GetParentProcessId()

Dim shell
    Set shell = WScript.CreateObject("WScript.Shell")

Dim input, i

    i=0
    Do While True
        WScript.StdOut.Write "Press space to stop process. Step [" & i & "]" & vbCR
        i = i + 1
        If (i Mod 10) = 0 Then 
            shell.AppActivate parentProcessId
            shell.SendKeys "~"
            input = WScript.StdIn.ReadLine()
            If input = " " Then
                Exit Do
            End If
        End If
        WScript.Sleep 50
    Loop

    WScript.StdOut.WriteLine vbCFLF & "Process ended"
    WScript.Quit

这篇关于需要在 VBscript 中感应击键以结束循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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