使用批处理文件运行循环时,是否可以检查按键按下情况? [英] Is it possible to check for a key press while a loop is running using a batchfile?

查看:149
本文介绍了使用批处理文件运行循环时,是否可以检查按键按下情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这一点很固执; 我需要将批处理(大多数其他程序类型限制在必须运行的系统上)运行大约10.000次,然后停止.

I'm quite stuck on this one; I need a batch (most other programtypes are restricted on the system this has to be run on) to run about 10.000 times, then stop.

我用这种方法走得很远:(在这里进行了概括和简化,但是会用技巧来解释东西,因为文件只需要点击一堆东西,就可以使人不快了)

I got this far with this method: (generalized and simplified here, yet would do the trick to explain stuff with, because the file just clicks a bunch of stuff, just inhumanly fast)

@echo off
set loop=0
:start
echo Hello world.
set /a loop=%loop%+1
if "%loop%"=="10000" goto exit
goto start
:exit
exit

上面的代码有效,我遇到的问题是风险. 很有可能,我需要的程序开始发送击键,如果循环仍未运行,这并不是什么大问题.该程序可能会以导致Windows崩溃的方式崩溃,这不是我所需要的.

The above code is working, the problem I'm having is the risk. There's a small probability, the program I need this for starts sending keystrokes, which isn't that big of a problem, if the loop wouldn't still be running. The program could crash in a way causing windows to crash, which isn't quite what i need.

所以我要问的问题是: 是否可以在循环运行时使用类似if keystrokeinput==anything goto exit的代码来停止它,还是我可以尝试另一种方法?

So the question I'm asking is: Is it possible to use something along the lines like if keystrokeinput==anything goto exit while the loop is running to stop it, or is there another method i could try?

如果没有任何效果,也许第三方程序可能会起作用?

If nothing works maybe a thirdpartyprogram that might do the trick?

我希望我的需求和问题很清楚

I hope my needs and problems are clear

击键需要像整个系统一样进行检查.如果该批次需要聚焦/打开并可见,则将无法使用.
需要从相应cmd外部检测击键,这意味着系统是否有任何输入.

The keystroke needs to be checked like systemwide. If the batch needs to be focused/opened and visible, it wouldn't work.
The keystroke needs to be detected from outside of the according cmd, meaning if there is any input to the system.

预先感谢

蒂姆

推荐答案

是的,可以通过运行多线程批处理来检测按键并用于退出循环.

Yes, keypress can be detected and used to exit a loop by running a multithreaded batch.

@Echo off

::: - Test if Key press thread is to be executed
    If /I "%~1" == "Wait" Goto :Wait

::: - Ensure previous Signal files removed in case program exited incorrectly
        Del /Q /F "%TEMP%\CMDrun.log"
        Del /Q /F "%TEMP%\CMDend.log"
    CLS

:Loop
::: - Launch second thread if not already running
    (IF Not Exist "%TEMP%\CMDrun.log" Start /B "" "%~F0" Wait) 2>Nul
    Set /A Count+=1
    Title %Count%
::: - Exit loop on second thread returning keypress
    (IF Exist "%TEMP%\CMDend.log" Goto :End_Loop) 2>Nul
If %Count% LSS 10000 Goto :Loop
:End_Loop
::: - Clean up Signal Files
    Echo. 
    Title Example Complete at count %count%
    Del /Q /F "%TEMP%\CMDrun.log"
    Del /Q /F "%TEMP%\CMDend.log"
    Pause
    Exit

:Wait
::: - Signal main thread run status
    >"%TEMP%\CMDrun.log" Echo Running
::: - Detect keypress
    For /F "Delims=" %%A in ('xcopy /w "%~f0" "%~f0" 2^>nul') do (
        If Not Defined Key Set "Key=%%A"
    )
::: - return keypress in 'end' signal file
    If defined key (
        Setlocal EnableDelayedExpansion
        Set "Key=!Key:~-1!"
        >"%TEMP%\CMDend.log" Echo !Key! && EXIT
        Endlocal
    )
::: - continue waiting for kepress
Goto :Wait

这篇关于使用批处理文件运行循环时,是否可以检查按键按下情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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