Is it possible to check for a key press while a loop is running using a batchfile?

爷,独闯天下 提交于 2020-07-23 06:59:05

问题


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

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.

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

Edit: 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.

Thanks in advance

Tim


回答1:


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



回答2:


When a batch file is interrupted via Ctrl-C, AKA STATUS_CONTROL_C_EXIT, it returns ErrorLevel -1073741510 or ExitCode C000013A. We can take advantage of this.

@echo off
echo Press Y to break
call:function

NEVER REACHED
exit /b

:function
REM WORKS inside cached command block!
(
    echo Loop still in progress...
    choice /C ny /T 1 /D n /N>x
    <x >nul 2>&1 call :checkify
)
goto:function


:checkify
cmd /c exit -1073741510


来源:https://stackoverflow.com/questions/61752062/is-it-possible-to-check-for-a-key-press-while-a-loop-is-running-using-a-batchfil

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!